无法读取属性' startsWith'在npm安装中为null

时间:2018-03-27 15:12:18

标签: node.js angular npm

我到处搜索这个问题。但仍然无法找到答案。大多数答案都说代理存在问题。当我尝试使用npm install -g package-name时发生错误。

npm ERR! Cannot read property 'startsWith' of null
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\User\AppData\Roaming\npm-cache\_logs\2018-03-27T14_55_06_994Z-debug.log

这是.log文件

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'install',
1 verbose cli   '-g',
1 verbose cli   '@angular/cli' ]
2 info using npm@5.6.0
3 info using node@v8.10.0
4 verbose npm-session bd0a5d284789daec
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 silly fetchPackageMetaData error for @angular/cli@latest Cannot read property 'startsWith' of null
8 verbose stack TypeError: Cannot read property 'startsWith' of null
8 verbose stack     at getProxy (C:\Program Files\nodejs\node_modules\npm\node_modules\pacote\node_modules\make-fetch-happen\agent.js:164:25)
8 verbose stack     at getAgent (C:\Program Files\nodejs\node_modules\npm\node_modules\pacote\node_modules\make-fetch-happen\agent.js:37:19)
8 verbose stack     at remoteFetch (C:\Program Files\nodejs\node_modules\npm\node_modules\pacote\node_modules\make-fetch-happen\index.js:310:17)
8 verbose stack     at opts.cacheManager.match.then.res (C:\Program Files\nodejs\node_modules\npm\node_modules\pacote\node_modules\make-fetch-happen\index.js:172:14)
8 verbose stack     at tryCatcher (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\util.js:16:23)
8 verbose stack     at Promise._settlePromiseFromHandler (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:512:31)
8 verbose stack     at Promise._settlePromise (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:569:18)
8 verbose stack     at Promise._settlePromise0 (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:614:10)
8 verbose stack     at Promise._settlePromises (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:693:18)
8 verbose stack     at Promise._fulfill (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:638:18)
8 verbose stack     at Promise._resolveCallback (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:432:57)
8 verbose stack     at Promise._settlePromiseFromHandler (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:524:17)
8 verbose stack     at Promise._settlePromise (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:569:18)
8 verbose stack     at Promise._settlePromise0 (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:614:10)
8 verbose stack     at Promise._settlePromises (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:693:18)
8 verbose stack     at Promise._fulfill (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:638:18)
9 verbose cwd C:\
10 verbose Windows_NT 10.0.16299
11 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "@angular/cli"
12 verbose node v8.10.0
13 verbose npm  v5.6.0
14 error Cannot read property 'startsWith' of null
15 verbose exit [ 1, true ]

版本 节点 - 8.10.0 npm - 5.6.0

5 个答案:

答案 0 :(得分:5)

经过两天的努力,我找到了答案。 在我的配置中没有代理设置在npm。这就是为什么当我尝试npm config get proxy时它为空。但是在我的电话中设置了两个环境变量http-proxy和https-proxy。但我不知道他们是如何被安排在那里的。所以我删除了那些变量。现在我的npm安装工作正常。

答案 1 :(得分:2)

我遇到此问题是因为我手动删除了https-proxy文件中的proxy.npmrc设置。我将其重新添加后,一切都可以正常工作。

答案 2 :(得分:0)

在使用Docker(v18.09.0)构建Angular 6映像时遇到了这个问题。

node和Docker环境的Proxy变量引起了问题。解决方法是按照以下说明重置httpProxy变量值:Docker doc。另外,将特定于项目的.npmrc设置为:gist并解决npm权限问题:gist

最后,我的Dockerfile如下:

FROM node:10.14.2-alpine
###
#Issue: 'Cannot read property 'startsWith' of null in npm install' error
#Reason: Proxy variables of node and Docker environment were causing the problem.
#Solution: Modify httpProxy variable value as told here: https://docs.docker.com/network/proxy/#configure-the-docker-client
# Also, set project specific .npmrc like : https://gist.github.com/nikhilbchilwant/7243ea4c6f35f28fb44376dac675d285

#RUN apk add tree

WORKDIR /home/node/aas-ui

#Fix for npm permission issue: https://github.com/angular/angular/blob/f8096d499324cf0961f092944bbaedd05364eea1/tools/ngcontainer/Dockerfile#L51
RUN mkdir /home/node/.npm-global \
 && npm config set prefix '/home/node/.npm-global' \
 && echo "export PATH=/home/node/.npm-global/bin:$PATH" >> /home/node/.profile
RUN source /home/node/.profile

RUN chown -R node $(npm config get prefix)
RUN chown -R node /home/node/aas-ui
USER node

COPY src ./src/
COPY e2e ./e2e/
COPY .angular-cli.json ./angular-cli.json
COPY protractor.conf.js ./protractor.conf.js
COPY karma.conf.js ./karma.conf.js
COPY tslint.json ./tslint.json
COPY package.json .
COPY angular.json .
COPY .npmrc .
RUN npm install --verbose
RUN npm build --verbose

我希望这对某人有帮助。

答案 3 :(得分:0)

也许您可以尝试 npm config set注册表https://registry.npmjs.org

答案 4 :(得分:0)

我必须设置我的 http 和 https 代理,以便 npm 可以获取包

npm config set proxy http://[IP]:[port]

npm config set https_proxy http://[IP]:[port]

然后npm i为我安装了所有软件包。

干杯!