执行npm install express时遇JSON解析错误的技术求助
你碰到的这个Unexpected end of JSON input错误,基本是npm缓存文件损坏或不完整导致的——大概率是之前的npm操作中途中断,使得缓存里的包元数据JSON文件没写完,解析时就触发了报错。
试试下面的步骤来修复:
- 先强制清理npm缓存:
npm cache clean --force - 缓存清理完成后,重新执行全局安装express的命令:
npm install -g express
如果上面的方法没生效,还能手动删除npm的缓存目录:
- 打开文件管理器,导航到
C:\Users\Rakshith Rakshak\AppData\Roaming\npm-cache(这是你系统里的缓存路径) - 删除整个
npm-cache文件夹,之后再重新运行npm install -g express
附上你提供的错误日志:
0 info it worked if it ends with ok
1 verbose cli [ 'D:\Program Files\nodejs\node.exe',
1 verbose cli 'C:\Users\Rakshith Rakshak\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js',
1 verbose cli 'install',
1 verbose cli '-g',
1 verbose cli 'express' ]
2 info using npm@6.9.2
3 info using node@v10.15.3
4 verbose npm-session 203510227a9454ec
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 http fetch GET 304 https://registry.npmjs.org/express 346ms (from cache)
8 silly fetchPackageMetaData error for express@latest Unexpected end of JSON input while parsing near
'..."dist":{"shasum":"e84'
9 timing stage:rollbackFailedOptional Completed in 1ms
10 timing stage:runTopLevelLifecycles Completed in 417ms
11 verbose stack SyntaxError: Unexpected end of JSON input while parsing near '..."dist":{"shasum":"e84'
11 verbose stack at JSON.parse ()
11 verbose stack at parseJson (C:\Users\Rakshith Rakshak\AppData\Roaming\npm\node_modules\npm\node_modules\json-parse-better-errors\index.js:7:17)
11 verbose stack at consumeBody.call.then.buffer (C:\Users\Rakshith
Rakshak\AppData\Roaming\npm\node_modules\npm\node_modules\node-fetch-npm\src\body.js:96:50)
11 verbose stack at process._tickCallback (internal/process/next_tick.js:68:7)
12 verbose cwd D:\My Files\node\role-based-auth
13 verbose Windows_NT 10.0.18362
14 verbose argv "D:\Program Files\nodejs\node.exe" "C:\Users\Rakshith
Rakshak\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js"
"install" "-g" "express"
15 verbose node v10.15.3
16 verbose npm v6.9.2
17 error Unexpected end of JSON input while parsing near '..."dist":{"shasum":"e84'
18 verbose exit [ 1, true ]
内容的提问来源于stack exchange,提问作者Rakshith Gajendra

