How to fix 'npm ERR! Cannot read property 'startsWith' of null'

核能气质少年 提交于 2021-01-28 04:52:01

问题


I am creating my first react-native app. I am attempting to install the react-native command line interface as shown here. I keep getting an error when I type the command to initiate the react-native command line

  • OS: Windows 10
  • Node: 10.15.3
  • npm: 6.4.1
  • I am not behind a proxy

  • I have removed the http and https environment variables

  • Updating node and npm creates a different error as shown here

Here's the error

C:\>npm install -g react-native-cli
npm ERR! Cannot read property 'startsWith' of null

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\dnort\AppData\Roaming\npm-cache\_logs\2019-04-15T09_52_28_346Z-debug.log

Here's npm get registry

C:\>npm get registry
https://registry.npmjs.org/

Here's npm config get proxy

C:\>npm config get proxy
null

Here's npm config get http-proxy

C:\>npm config get http-proxy
undefined

Here's npm config get https-proxy

C:\>npm config get https-proxy
null

Here's the log file

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   'react-native-cli' ]
2 info using npm@6.4.1
3 info using node@v10.15.3
4 verbose npm-session 8e6a6f9ef2e9298d
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 silly fetchPackageMetaData error for react-native-cli@latest Cannot read property 'startsWith' of null
8 timing stage:rollbackFailedOptional Completed in 7ms
9 timing stage:runTopLevelLifecycles Completed in 1811ms
10 verbose stack TypeError: Cannot read property 'startsWith' of null
10 verbose stack     at getProxy (C:\Program Files\nodejs\node_modules\npm\node_modules\make-fetch-happen\agent.js:164:25)
10 verbose stack     at getAgent (C:\Program Files\nodejs\node_modules\npm\node_modules\make-fetch-happen\agent.js:37:19)
10 verbose stack     at remoteFetch (C:\Program Files\nodejs\node_modules\npm\node_modules\make-fetch-happen\index.js:310:17)
10 verbose stack     at opts.cacheManager.match.then.res (C:\Program Files\nodejs\node_modules\npm\node_modules\make-fetch-happen\index.js:172:14)
10 verbose stack     at tryCatcher (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\util.js:16:23)
10 verbose stack     at Promise._settlePromiseFromHandler (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:512:31)
10 verbose stack     at Promise._settlePromise (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:569:18)
10 verbose stack     at Promise._settlePromise0 (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:614:10)
10 verbose stack     at Promise._settlePromises (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:693:18)
10 verbose stack     at Promise._fulfill (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:638:18)
10 verbose stack     at Promise._resolveCallback (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:432:57)
10 verbose stack     at Promise._settlePromiseFromHandler (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:524:17)
10 verbose stack     at Promise._settlePromise (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:569:18)
10 verbose stack     at Promise._settlePromise0 (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:614:10)
10 verbose stack     at Promise._settlePromises (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:693:18)
10 verbose stack     at Promise._fulfill (C:\Program Files\nodejs\node_modules\npm\node_modules\bluebird\js\release\promise.js:638:18)
11 verbose cwd C:\
12 verbose Windows_NT 10.0.17134
13 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "react-native-cli"
14 verbose node v10.15.3
15 verbose npm  v6.4.1
16 error Cannot read property 'startsWith' of null
17 verbose exit [ 1, true ]

I expect to install the react-native command line to be able to initiate react-native projects.

EDIT Please see the npm config ls -l input below

C:\Windows\System32>npm config ls -l
; cli configs
long = true
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.4.1 node/v10.15.3 win32 x64"

; userconfig C:\Users\dnort\.npmrc
https-proxy = null
proxy = null
strict-ssl = false

; builtin config undefined
prefix = "C:\\Users\\dnort\\AppData\\Roaming\\npm"

; default values
access = null
allow-same-version = false
also = null
always-auth = false
audit = true
audit-level = "low"
auth-type = "legacy"
bin-links = true
browser = null
ca = null
cache = "C:\\Users\\dnort\\AppData\\Roaming\\npm-cache"
cache-lock-retries = 10
cache-lock-stale = 60000
cache-lock-wait = 10000
cache-max = null
cache-min = 10
cafile = undefined
cert = null
cidr = null
color = true
commit-hooks = true
depth = null
description = true
dev = false
dry-run = false
editor = "notepad.exe"
engine-strict = false
fetch-retries = 2
fetch-retry-factor = 10
fetch-retry-maxtimeout = 60000
fetch-retry-mintimeout = 10000
force = false
git = "git"
git-tag-version = true
global = false
global-style = false
globalconfig = "C:\\Users\\dnort\\AppData\\Roaming\\npm\\etc\\npmrc"
globalignorefile = "C:\\Users\\dnort\\AppData\\Roaming\\npm\\etc\\npmignore"
group = 0
ham-it-up = false
heading = "npm"
https-proxy = null
if-present = false
ignore-prepublish = false
ignore-scripts = false
init-author-email = ""
init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "C:\\Users\\dnort\\.npm-init.js"
init-version = "1.0.0"
json = false
key = null
legacy-bundling = false
link = false
local-address = undefined
loglevel = "notice"
logs-max = 10
; long = false (overridden)
maxsockets = 50
message = "%s"
; metrics-registry = null (overridden)
node-options = null
node-version = "10.15.3"
noproxy = null
offline = false
onload-script = null
only = null
optional = true
otp = null
package-lock = true
package-lock-only = false
parseable = false
prefer-offline = false
prefer-online = false
; prefix = "C:\\Program Files\\nodejs" (overridden)
preid = ""
production = false
progress = true
proxy = null
read-only = false
rebuild-bundle = true
registry = "https://registry.npmjs.org/"
rollback = true
save = true
save-bundle = false
save-dev = false
save-exact = false
save-optional = false
save-prefix = "^"
save-prod = false
scope = ""
script-shell = null
scripts-prepend-node-path = "warn-only"
searchexclude = null
searchlimit = 20
searchopts = ""
searchstaleness = 900
send-metrics = false
shell = "C:\\WINDOWS\\system32\\cmd.exe"
shrinkwrap = true
sign-git-commit = false
sign-git-tag = false
sso-poll-frequency = 500
sso-type = "oauth"
; strict-ssl = true (overridden)
tag = "latest"
tag-version-prefix = "v"
timing = false
tmp = "C:\\Users\\dnort\\AppData\\Local\\Temp"
umask = 0
unicode = false
unsafe-perm = true
update-notifier = true
usage = false
user = 0
; user-agent = "npm/{npm-version} node/{node-version} {platform} {arch}" (overridden)
userconfig = "C:\\Users\\dnort\\.npmrc"
version = false
versions = false
viewer = "browser"

回答1:


There was no proxy set in my configs in npm. I tried npm config get proxy it was null.

Try this link




回答2:


Solution found

  1. Uninstall current version of node.js and npm from C:\Users{your_username}\AppData\Roaming
  2. Install an older version of node.js. You can do that here. At the time of this post I installed the LTS (Node-10.15.3 & npm-6.4.1).
  3. Remove http & https environment variables. (type in environment variables in search bar which should take your straight to system variables).
  4. Temporarily disable windows defender features.
  5. Restart system
  6. Open administrator node.js command prompt
  7. Enter npm install -g react-native-cli

My result output is now as expected:

C:\Windows\System32>npm install -g react-native-cli
C:\Users\dnort\AppData\Roaming\npm\react-native -> C:\Users\dnort\AppData\Roaming\npm\node_modules\react-native-cli\index.js
+ react-native-cli@2.0.1
added 41 packages from 15 contributors in 19.22s

This post is a continuation of my 'How to fix 'npm does not support Node.js v11.14.0 error' on Windows?' here

Note: Most solutions to similar problems have been related to proxy issues, unsupported versions of node.js/npm, environment variables




回答3:


this may occur cause of coding error, in my case this line caused error

<img class="cat-icon" v-bind:src="require('img/icon/' + icon)" style="width:25px;" />

try to run "npm run watch" to see detailed error




回答4:


I ran in to similar problem which was caused by wrong ralative path when registring components by require.context




回答5:


I have tried a variety of commands to delete proxy settings. I found that the best way to clear out proxy settings is to clear out related entry in .npmrc. In my case, I simply empty the whole file and restart cmd. And it works.




回答6:


This helped me:

npm config set proxy http://username:password@host:port
npm config set https-proxy http://username:password@host:port


来源:https://stackoverflow.com/questions/55686843/how-to-fix-npm-err-cannot-read-property-startswith-of-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!