node app fails to run on mojave: ReferenceError: internalBinding is not defined

node app fails to run on mojave: ReferenceError: internalBinding is not defined

我尝试 运行 一个节点应用程序,但 运行 在 High Sierra 和 Windows 10 上都很好,但在 Mojave 10.14.1 上失败了。 这是 运行 执行 gulp build_dev 任务时显示的错误:

[23:44:42] Requiring external module babel-register
fs.js:25
'use strict';
^

ReferenceError: internalBinding is not defined
    at fs.js:25:1
    at req_ (/Users/user1/Documents/NodeProjects/myapp/node_modules/natives/index.js:137:5)
    at Object.req [as require] (/Users/user1/Documents/NodeProjects/myapp/node_modules/natives/index.js:54:10)
    at Object.<anonymous> (/Users/user1/Documents/NodeProjects/myapp/node_modules/vinyl-fs/node_modules/graceful-fs/fs.js:1:37)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/user1/Documents/NodeProjects/myapp/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! myapp@1.0.0 dev: `node node_modules/gulp/bin/gulp.js build_dev`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the myapp@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/user1/.npm/_logs/2018-11-04T22_44_42_709Z-debug.log

这是 package.json 文件的内容,其中包含应用程序中使用的所有依赖项:

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test-dev": "node node_modules/gulp/bin/gulp.js test_dev",
    "test-prod": "node node_modules/gulp/bin/gulp.js test_prod",
    "dev": "node node_modules/gulp/bin/gulp.js build_dev",
    "prod": "node node_modules/gulp/bin/gulp.js build_prod"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-polyfill": "^6.26.0",
    "body-parser": "^1.17.2",
    "cookie-parser": "^1.4.3",
    "envify": "^4.1.0",
    "express": "^4.16.2",
    "glob": "^7.1.2",
    "http-status-codes": "^1.1.6",
    "morgan": "^1.8.2",
    "multi-glob": "^1.0.1",
    "npm": "^6.4.1",
    "path": "^0.12.7"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-core": "^6.26.0",
    "babel-plugin-syntax-async-functions": "^6.13.0",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-preset-env": "^1.6.0",
    "babel-register": "^6.24.1",
    "babelify": "^7.3.0",
    "browser-sync": "^2.26.3",
    "browserify": "^16.1.0",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.1",
    "gulp-if": "^2.0.2",
    "gulp-imagemin": "^3.3.0",
    "gulp-minify-css": "^1.2.4",
    "gulp-nodemon": "^2.2.1",
    "gulp-notify": "^3.0.0",
    "gulp-rename": "^1.2.2",
    "gulp-sass": "^3.1.0",
    "gulp-sass-glob": "^1.0.8",
    "gulp-sourcemaps": "^2.6.0",
    "gulp-uglify": "^3.0.0",
    "gulp-util": "^3.0.8",
    "imagemin-pngquant": "^5.0.1",
    "run-sequence": "^2.2.0",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0"
  }
}

关于如何调试这个问题的任何提示?可能与 gcc 编译器版本有关?

节点版本:11.1.0 npm 版本:6.4.1

在此link我上传了"npm install"命令的日志以安装依赖项:npm_install.log

尝试将 natives 更新到最新版本。 This 提交似乎可以解决您遇到的问题。

我将 Node 版本从 10.15.0(LTS) 降级到 10.10.0。这为我修复了 internalBinding 错误。

sudo npm cache clean -f
sudo npm install -g n
sudo n 10.10.0

就我而言,问题出在 node_modules 安装上,因此请执行以下操作。

  • 执行 yarn audit
  • 检查您是否存在严重漏洞
  • 升级模块版本或搜索替代模块。
  • yarn install
  • 你应该可以开始了

注意:在我的案例中,问题不在于节点版本,它只是一个易受攻击的模块(gulp-angular-templatecache)。如果上述方法不起作用,我还建议您执行 npm upgradenpm rebuild

npm audit 不适用于私有存储库,但 yarn audit 可以。