“npm ci”引发异常:无法读取未定义的属性“长度”

Val*_*llo 5 npm circleci

我在本地环境和 CircleCI 中都遇到了这个异常。

> core-js@3.6.4 postinstall /home/vallo/project/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

npm ERR! Cannot read property 'length' of undefined
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vallo/.npm/_logs/2020-01-16T20_12_00_564Z-debug.log
Run Code Online (Sandbox Code Playgroud)

/home/vallo/.npm/_logs/2020-01-16T20_12_00_564Z-debug.log 最后几行:

11424 信息生命周期@babel/core@7.8.3~postinstall: @babel/core@7.8.3

11425 信息生命周期 undefined@undefined~install: undefined@undefined

11426 详细堆栈类型错误:无法读取未定义的属性“长度”

11426 详细堆栈在 _incorrectWorkingDirectory (/home/vallo/.nvm/versions/node/v12.14.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:114:60)

11426 详细堆栈位于 /home/vallo/.nvm/versions/node/v12.14.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:86:44

11426 详细堆栈位于 /home/vallo/.nvm/versions/node/v12.14.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:218:12

11426 回调中的详细堆栈 (/home/vallo/.nvm/versions/node/v12.14.0/lib/node_modules/npm/node_modules/graceful-fs/polyfills.js:295:20)

FSReqCallback.oncomplete (fs.js:159:5) 上的 11426 详细堆栈

11427 详细的 cwd /home/vallo/project

11428 详细的 Linux 5.3.0-26-generic

11429 详细 argv“/home/vallo/.nvm/versions/node/v12.14.0/bin/node”“/home/vallo/.nvm/versions/node/v12.14.0/bin/npm”“ci”

11430 详细节点 v12.14.0

11431 详细的 npm v6.13.6

11432 错误无法读取未定义的属性“长度”

11433 详细退出 [1,真]

这是我的 package.json:

{
  "private": true,
  "scripts": {
    "install": "npx babel-node scripts/npm/install.js",
    "test": "npx babel-node scripts/npm/test.js",
    "lambda-test": "lambda/serverUnit.sh",
    "lint": "node_modules/.bin/eslint .",
    "flow": "flow; test $? -eq 0 -o $? -eq 2"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.4",
    "@babel/node": "^7.2.2",
    "@babel/plugin-proposal-object-rest-spread": "^7.5.5",
    "@babel/plugin-transform-modules-commonjs": "^7.4.4",
    "@babel/plugin-transform-runtime": "^7.4.4",
    "@babel/polyfill": "^7.4.4",
    "@babel/preset-env": "^7.4.4",
    "@babel/preset-flow": "^7.0.0",
    "@babel/register": "^7.4.4",
    "@sanjo/jasmine-expect": "^1.0.1",
    "@sanjo/jasmine-spy": "^1.0.1",
    "@share911/babel-plugin-root-slash-import": "^1.2.0",
    "aws-sdk": "^2.521.0",
    "babel-eslint": "^10.0.3",
    "babel-loader": "^8.0.5",
    "babel-plugin-meteor-imports": "^1.0.3",
    "babelify": "^10.0.0",
    "browserify": "^16.2.3",
    "chai": "^4.2.0",
    "commander": "2.9.0",
    "eslint": "^6.3.0",
    "eslint-config-standard": "^14.1.0",
    "eslint-plugin-flowtype": "^4.3.0",
    "eslint-plugin-import": "^2.18.2",
    "eslint-plugin-node": "^10.0.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-react": "^7.14.3",
    "eslint-plugin-standard": "^4.0.1",
    "flow-bin": "^0.106.3",
    "jsdom": "^15.1.1",
    "jsdom-global": "^3.0.2",
    "json-loader": "^0.5.4",
    "karma": "^4.1.0",
    "karma-browserify": "^6.0.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-mocha": "^1.3.0",
    "meteor": "./modules/meteor/",
    "meteor-promise": "^0.8.0",
    "mocha": "^6.1.4",
    "phantomjs-prebuilt": "^2.1.15",
    "proxyquire": "^1.7.10",
    "proxyquireify": "^3.2.0",
    "serverless": "^1.51.0",
    "sinon": "^7.3.2",
    "standard": "^12.0.1",
    "tingodb": "^0.6.1",
    "wallabify": "0.0.14",
    "watchify": "^3.11.1",
    "webpack": "^4.30.0",
    "webpack-node-externals": "^1.7.2"
  },
  "dependencies": {
    "@babel/runtime": "^7.4.5",
    "aws-xray-sdk-core": "^2.3.5",
    "chai-as-promised": "^7.1.1",
    "json-to-pretty-yaml": "^1.2.2",
    "obj-resolve": "^1.0.4",
    "winston": "^3.2.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在运行节点 12.14.0 和 npm 6.13.6

Val*_*llo 26

问题出在函数_incorrectWorkingDirectoryfrom上npm-lifecycle,它不检查是否pkg.name为 null,因此会引发异常。

要解决此问题,请将“name”属性添加到package.json.

https://github.com/npm/npm-lifecycle/blob/latest/index.js#L114

  • 名称不应该是必需的,它是一个错误,应该报告,但我未能创建一个可复制的最小示例。如果您设法做到这一点,请将其作为问题添加到我在答案中发布的 github 链接上,以便它得到修复。 (3认同)
  • 非常感谢。我仍然不明白如果我不发布包,为什么需要名称......但至少我的构建现在会通过 (2认同)
  • 太棒了,非常适合部署到 herokuapp(laravel 和 nodejs) (2认同)