Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import Brain.JS into my NodeJS script

I'm trying to write a neural network using brain.js. However, NodeJS doesn't allow me to do that neither with require('brain.js') nor with import 'brain.js'. Here is my code:

const brain = require('brain.js')

const config = {
    iterations: 15000,
    log: true,
    logPeriod: 500,
    layers: [10]
}

const net = new brain.recurrent.LSTM()

const conjunctions = {
    0: 'Именительный',
    1: 'Родительный',
    2: 'Дательный',
    3: 'Винительный',
    4: 'Творительный',
    5: 'Предложный'
}

const inputData = [
    // some data for training
]

const test = 'Дельфина'

net.train(inputData)
const output = net.run(test)
console.log(`${conjunctions[output]} падеж`)

However, it shows me next error when running with require('brain.js'):

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:         c:\Coding\Labs\Node.JS\MochaTest\node_modules\brain.js\src\index.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1156:13)
    at Module.load (internal/modules/cjs/loader.js:976:32)
    at Function.Module._load (internal/modules/cjs/loader.js:884:14)
    at Module.require (internal/modules/cjs/loader.js:1016:19)
    at require (internal/modules/cjs/helpers.js:69:18)
    at Object.<anonymous> (c:\Coding\Labs\Node.JS\MochaTest\index.js:1:15)
    at Module._compile (internal/modules/cjs/loader.js:1121:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
    at Module.load (internal/modules/cjs/loader.js:976:32)
    at Function.Module._load (internal/modules/cjs/loader.js:884:14) {
      code: 'ERR_REQUIRE_ESM'

}

And next error when running with import * as brain from 'brain.js':

(node:2648) ExperimentalWarning: The ESM module loader is experimental.
file:///c:/Coding/Labs/Node.JS/MochaTest/node_modules/brain.js/src/index.js:1
const activation = require('./activation');
                   ^

ReferenceError: require is not defined
    at file:///c:/Coding/Labs/Node.JS/MochaTest/node_modules/brain.js/src/index.js:1:20
    at ModuleJob.run (internal/modules/esm/module_job.js:109:37)
    at async Loader.import (internal/modules/esm/loader.js:133:24)

What should I do and how to import it right when using NodeJS 13.2.0? I have only brain.js and Mocha installed in this module.

like image 317
SM Melamed Avatar asked Dec 19 '25 19:12

SM Melamed


1 Answers

You can import brainjs like that :

import brainjs from 'brain.js/src'

As mentioned here : https://github.com/BrainJS/brain.js/issues/80

like image 158
Alice Oualouest Avatar answered Dec 21 '25 07:12

Alice Oualouest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!