Kokodoko
Kokodoko

Reputation: 28128

Brain.js import not found

I must be missing a basic setup thing, but I cannot get the examples on brainJS working.

The examples mention importing a module and then creating a neural net:

import brain from 'brain.js';
const lstm = new brain.recurrent.LSTM();

But, when copy>pasting the tutorials, or trying any other path to the brain.js folder, I get this error, with brain underlined in the terminal:

import brain from 'node_modules/brain.js/index.js'

SyntaxError: Unexpected identifier

My directory structure

app.js
- node_modules (folder)
--- brain.js (folder)
------index.js

It seems that there is only a "brain.js" folder but not a file. I have tried importing with:

 import brain from 'node_modules/brain.js/index.js';
 import brain from 'node_modules/brain.js/';
 import brain from './brain.js/index.js';

etc. etc.

Upvotes: 3

Views: 3316

Answers (3)

user11775345
user11775345

Reputation:

If you are using the browser use this code in order to allow the browser to use the browser friendly version of the source code Brain.js

<script src="https://cdn.rawgit.com/BrainJS/brain.js/45ce6ffc/browser.js"></script>

Upvotes: 1

Robert Plummer
Robert Plummer

Reputation: 652

import * as brain from 'brain.js';

Q: Why does this not work?

import brain from 'brain.js';

A: Javascript's ES6 import statement is looking for an ES6 module with a default export. Brain doesn't use this export, it makes little sense since there are more than one neural network type. Here is an example using default export:

export default {};

Q: Can imports be used with older javascript libraries?

A: Absolutely. As stated in the answer: import * as library from 'library-name';

Upvotes: 2

H. Majury
H. Majury

Reputation: 376

Are you using Node.js? If so, I believe import isn't supported yet. If you are using Node, you should use:

const brain = require('brain.js');

I tried this and it works fine. Hope this helps.

Upvotes: 4

Related Questions