我正在终端上运行一个节点应用程序。最近已经升级到节点v8.5.0,但我得到这个错误:

Error: The module '/tidee/tidee-au/packages/tidee-au-server/node_modules/bcrypt/lib/binding/bcrypt_lib.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 51. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Object.Module._extensions..node (module.js:653:18)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/tidee/tidee-au/packages/tidee-au-server/node_modules/bcrypt/bcrypt.js:6:16)
    at Module._compile (module.js:624:30)
    at Module._extensions..js (module.js:635:10)
    at Object.require.extensions.(anonymous function) [as .js] (/tidee/tidee-au/packages/tidee-au-server/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/tidee/tidee-au/packages/tidee-au-server/server/helpers/encryptPass.js:1:16)

知道怎么解决吗?


当前回答

结果我的问题是用户错误:确保你正在运行的节点版本与你在运行npm install或yarn时使用的是相同的。

我使用NVM对节点进行版本控制,并通过终端运行yarn,但我的IDE在运行时设置为使用旧版本的节点,并抛出上面的错误。在运行配置中匹配我的IDE的节点版本到node——version修复了这个问题。

其他回答

I have hit this error twice in an electron app and it turned out the problem was that some modules need to be used from the main process rather than the render process. The error occurred using pdf2json and also node-canvas. Moving the code that required those modules from index.htm (the render process) to main.js (the main process) fixed the error and the app rebuilt and ran perfectly. This will not fix the problem in all cases but it is the first thing to check if you are writing an electron app and run into this error.

我在使用robotjs时也遇到了类似的问题。有一些废弃的代码需要节点v11,但我已经在v12上编译了电子代码。所以我得到了同样的错误。 这里没有任何工作,因为我基本上是试图将电子和其他依赖项从v12重建到节点v11。

以下是我所做的(部分是基于chitzui的回答,功劳在该有的地方):

备份package.json 完全删除node_modules文件夹 完全删除package_lock.json 删除包。Json(稍后将重新编译) 关闭项目目录下所有打开的编辑器和其他cmd窗口。 运行NPM init重新安装包,然后丢失旧备份的package.json中的数据 运行NPM I 固定

检查您正在使用的Node版本,可能与预期的不匹配。

您需要从node_modules文件夹中删除模块文件夹(bcrypt)并重新安装,使用以下命令:

$ rm -rf node_modules/bcrypt
$ npm install
// or
$ yarn

在尝试了不同的东西之后。 这工作。

删除节点模块文件夹并运行

npm i