javascript - 无法将 Brain.JS 导入我的 NodeJS 脚本

标签 javascript node.js

我正在尝试使用 Brain.js 编写神经网络。但是,NodeJS 不允许我使用 require('brain.js')import 'brain.js' 来执行此操作。 这是我的代码:

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]} падеж`)

但是,当使用 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'

}

使用 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)

使用 NodeJS 13.2.0 时我应该做什么以及如何正确导入它?我在这个模块中只安装了 Brain.js 和 Mocha。

最佳答案

你可以像这样导入brainjs:

import brainjs from 'brain.js/src'

正如这里提到的:https://github.com/BrainJS/brain.js/issues/80

关于javascript - 无法将 Brain.JS 导入我的 NodeJS 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096491/

相关文章:

php - 找不到命令 : PHP exec()

javascript - Ruby on Rails 生成 Json

javascript - md-form-field 不是已知元素 Angular Material

javascript - Sails.js + grunt 不编译 Assets

mysql - 是否可以将本地 mysql 与 heroku 上的 nodejs 连接?

javascript - 从 Node.js 连接 Azure 分析服务

javascript - 如何使用 MySQL 登录 ReactJS、NodeJS?

javascript - 学习编程 JavaScript,但我被卡住了

php - 在 Symfony2 运行时包含 Zepto 或 jQuery

javascript - 通过 mongo 聚合管道使用 $match 两次