question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not possible to require or import?

See original GitHub issue

I switched to this maintained library from the old one. Just doing the switch, I am no longer able to use a standard const brain = require('brain.js') (I basically just changed from brain to brain.js in the require), because the module will not be exported but put on the global window variable.

Is there a reason for this? For now I just access the library using window.brain, but it would be nice to actually import/require the module to achieve a modern build process using e.g. webpack.

Any info on this and explanation why it is done this way would be nice.

//edit: I’m using brain inside the browser, I also tried brain.js/browser and it is also adding brain to window instead of exporting it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:24 (11 by maintainers)

github_iconTop GitHub Comments

6reactions
robertleeplummerjrcommented, Aug 4, 2017

I believe this is now resolved. @michaelsanford the library uses (at least I looked) import { xxx } from whatever;, (src/neural-network.js, for example) but does not in index.js which gets files from ./dist, which are es5.

If you’d like to use the es6 files, simply use import brain from 'brain.js/src'; or import brain from 'brain.js/src/index'; I think.

@robertleeplummerjr If you’ve not yet gotten around to it, I’d be interested in working on this.

I didn’t see that till it was too late. Feel free to check the wiki, the direction, and any outstanding bugs. Committers & forks are wanted and loved.

Ty!

3reactions
matthewjumpsoffbuildingscommented, Sep 28, 2021

When using in node, I’m still getting TypeError: brain.NeuralNetwork is not a constructor when I do

const brain = require('brain.js')
const net = new brain.NeuralNetwork()

It turns out that, for some bizzare reason, the export from brain.js is this

{
  brain: { ...brain class },
  default: { ...brain class again? }
}

So in order to get it working properly, you should do

const brain = require('brain.js').brain // redundant access to nested object
const net = new brain.NeuralNetwork()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Both Require and import not working javascript - Stack Overflow
For some reason I can't figure out I'm unable to use either require or import when trying to import the Chalk package for...
Read more >
JavaScript require vs import - Flexiple
One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() ...
Read more >
JavaScript Require vs. Import - Bits and Pieces
With modern web development, we use require or import to refer to JavaScript dependencies. And, sometimes, we use both in combination and use ......
Read more >
Compare require() vs import() in JavaScript | by Sumeet Bhalla
Both require() and import() are used to include modules. But they have several important features you should be aware of.
Read more >
Detect require vs. import based on the entry point, not ... - GitHub
any instance of import during the run is now a perfectly normal "import is a reserved keyword" error. any instance of the dynamic...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found