如何修复npm ERR!无法读取属性'startsWith'为空'

时间:2019-04-15 10:08:12

标签: javascript node.js npm command-line chocolatey

我正在创建我的第一个React-native应用程序。我正在尝试安装here所示的react-native命令行界面。键入命令以启动react-native命令行时,我总是收到错误消息

  • 操作系统:Windows 10
  • 节点:10.15.3
  • npm:6.4.1
  • 我没有代理人

  • 我已经删除了http和https环境变量

  • 更新节点和npm会产生不同的错误,如图here

这是错误

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

这是npm获取注册表

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

这是npm config获取代理

C:\>npm config get proxy
null

这里的npm配置获取http-proxy

C:\>npm config get http-proxy
undefined

这里的npm配置获取https-proxy

C:\>npm config get https-proxy
null

这是日志文件

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 ]

我希望安装react-native命令行以能够启动react-native项目。

编辑 请参阅下面的npm config ls -l输入

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"

6 个答案:

答案 0 :(得分:1)

在npm中,我的配置中没有设置代理。我尝试nmp config get proxy为空。

Try this link

谢谢

答案 1 :(得分:1)

找到解决方案

  1. 从C:\ Users {您的用户名} \ AppData \ Roaming卸载当前版本的node.js和npm
  2. 安装旧版本的node.js。您可以这样做here。在撰写本文时,我安装了LTS(Node-10.15.3和npm-6.4.1)。
  3. 删除http和https环境变量。 (在搜索栏中输入环境变量,可以直接进入系统变量。)
  4. 暂时禁用Windows Defender功能。
  5. 重启系统
  6. 打开管理员node.js命令提示符
  7. 输入npm install -g react-native-cli

我的结果输出现在符合预期:

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

本文是我的“如何解决Windows上的'npm不支持Node.js v11.14.0错误'的续篇?”的继续。 here

注意:大多数类似问题的解决方案都与代理问题,不支持的node.js / npm版本,环境变量有关

答案 2 :(得分:1)

这可能是导致编码错误的原因,在我的情况下,此行导致了错误

        import { Router } from '@angular/router';

    //-------------------------------------------------//

    /**
     * Replaces everything after the last '/' in currentUrl with replacementSegment.    
     * Will also remove any paramaters. 
     * @param currentUrl url to change
     * @param replacementSegment what to put at the end of the altered url
     * @returns the altered url
     */
    export function ReplaceLastUrlSegment(currentUrl: string, replacementSegment: string): string {

        if (!currentUrl)
            return '/' + replacementSegment

        //remove any paramaters
        const segments = currentUrl.split('?')
        if (!segments?.length)
            return '/' + replacementSegment

        return segments[0].split('/').slice(0, -1).join('/') + '/' + replacementSegment


    }//ReplaceLastUrlSegment

    //-------------------------------------------------//

    /**
     * Replaces everything after the last '/' in router.url with replacementSegment.    
     * Will also remove any paramaters. 
     * @param router current router
     * @param replacementSegment what to put at the end of the altered url
     * @returns the router's altered url
     */
    export function ReplaceLastUrlSegmentFromRouter(router: Router, replacementSegment: string): string {


        //Don't null check it.
        //It should crash if router is null
        return ReplaceLastUrlSegment(router.url, replacementSegment)

    }//ReplaceLastUrlSegmentFromRouter

    //-------------------------------------------------//

    /**
     * Replaces everything after the last '/' in router.url with replacementSegment.  Then navigates to new url.
     * Will also remove any paramaters. 
     * @param router current router
     * @param replacementSegment what to put at the end of the altered url
     */
    export function ReplaceLastUrlSegmentAndNavigate(router: Router, replacementSegment: string) {

        //Don't null check it.
        //It should crash if router is null

        const newUrl = ReplaceLastUrlSegment(router?.url, replacementSegment)
        router.navigateByUrl(newUrl)


    }//ReplaceLastUrlSegmentFromRouter

    //-------------------------------------------------//

尝试运行“ npm run watch”以查看详细错误

答案 3 :(得分:0)

我遇到了类似的问题,该问题是由require.context注册组件时错误的相对路径引起的

答案 4 :(得分:0)

我尝试了各种命令来删除代理设置。 我发现清除代理设置的最佳方法是清除.npmrc中的相关条目。 就我而言,我只是清空整个文件并重新启动cmd。而且有效。

答案 5 :(得分:0)

这对我有帮助:

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