"npm ci" 抛出异常:无法读取未定义的 属性 'length'

"npm ci" throws exception: Cannot read property 'length' of undefined

我在本地环境和 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

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

11424 info lifecycle @babel/core@7.8.3~postinstall: @babel/core@7.8.3

11425 info lifecycle undefined@undefined~install: undefined@undefined

11426 verbose stack TypeError: Cannot read property 'length' of undefined

11426 verbose stack at _incorrectWorkingDirectory (/home/vallo/.nvm/versions/node/v12.14.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:114:60)

11426 verbose stack at /home/vallo/.nvm/versions/node/v12.14.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:86:44

11426 verbose stack at /home/vallo/.nvm/versions/node/v12.14.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:218:12

11426 verbose stack at callback (/home/vallo/.nvm/versions/node/v12.14.0/lib/node_modules/npm/node_modules/graceful-fs/polyfills.js:295:20)

11426 verbose stack at FSReqCallback.oncomplete (fs.js:159:5)

11427 verbose cwd /home/vallo/project

11428 verbose Linux 5.3.0-26-generic

11429 verbose argv "/home/vallo/.nvm/versions/node/v12.14.0/bin/node" "/home/vallo/.nvm/versions/node/v12.14.0/bin/npm" "ci"

11430 verbose node v12.14.0

11431 verbose npm v6.13.6

11432 error Cannot read property 'length' of undefined

11433 verbose exit [ 1, true ]

这是我的 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"
  }
}

我是 运行 节点 12.14.0 和 npm 6.13.6

问题出在 npm-lifecycle 的函数 _incorrectWorkingDirectory 上,它不检查 pkg.name 是否为空,因此抛出异常。

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

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