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



我正在尝试使用大脑编写神经网络.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:CodingLabsNode.JSMochaTestnode_modulesbrain.jssrcindex.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:CodingLabsNode.JSMochaTestindex.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 时,我应该怎么做以及如何正确导入它?我只有大脑.js和摩卡安装在这个模块中。

你可以像这样导入 brainjs:

import brainjs from 'brain.js/src'

如前所述: https://github.com/BrainJS/brain.js/issues/80

查找了一些教程后,我认为您可以使用const brain = require('brain')导入它。

应该安装大脑JS,你可以用npm来做,但请记住大脑确实有系统依赖性, 通常人们会忘记安装python(我不知道你的系统,但是当你开始使用它们时,很多服务器都是"空的"(

Mac OS X 蟒蛇 2.7 XCode

Ubuntu/Debian 蟒蛇 2.7 一个 GNU C++ 环境(可通过 apt 上的 build-essential 软件包获得( libxi-dev 工作和最新的 OpenGL 驱动程序 格鲁 pkg-config

窗户 蟒蛇 2.7 Microsoft Visual Studio Build Tools 2015 在 CMD 中运行:2015 msvs_version NPM 配置集 在CMD中运行:npm config set Python python2.7

在此处输入图像描述

只需删除包中的"type":"module".json

最新更新