npm安装react、react-dom、next失败,报EJSONPARSE错误求助
问题场景
项目仅包含index.js和空的package.json文件,执行安装命令:
npm install react@latest react-dom@latest next@latest
终端返回EJSONPARSE错误,提示解析空package.json失败。
错误详情
终端报错内容
npm ERR! code EJSONPARSE
npm ERR! path C:\WEBSITES\Projets de la premiere cliente\platformes vente places d'installation/package.json
npm ERR! JSON.parse Unexpected end of JSON input while parsing empty string
npm ERR! JSON.parse Failed to parse JSON data.
npm ERR! JSON.parse Note: package.json must be actual JSON, not just JavaScript.npm ERR! A complete log of this run can be found in: C:\Users\HP\AppData\Local\npm-cache_logs\2024-01-26T03_19_04_697Z-debug-0.log
完整错误日志
0 verbose cli C:\Program Files\nodejs\node.exe C:\Users\HP\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js 1 info using npm@10.2.1 2 info using node@v18.18.0 3 timing npm:load:whichnode Completed in 4ms 4 timing config:load:defaults Completed in 8ms 5 timing config:load:file:C:\Users\HP\AppData\Roaming\npm\node_modules\npm\npmrc Completed in 22ms 6 timing config:load:builtin Completed in 22ms 7 timing config:load:cli Completed in 4ms 8 timing config:load:env Completed in 2ms 9 timing config:load:file:C:\WEBSITES\Projets de la premiere cliente\platformes vente places d'installation\.npmrc Completed in 1ms 10 timing config:load:project Completed in 3ms 11 timing config:load:file:C:\Users\HP\.npmrc Completed in 0ms 12 timing config:load:user Completed in 1ms 13 timing config:load:file:C:\Users\HP\AppData\Roaming\npm\etc\npmrc Completed in 1ms 14 timing config:load:global Completed in 1ms 15 timing config:load:setEnvs Completed in 2ms 16 timing config:load Completed in 43ms 17 timing npm:load:configload Completed in 44ms 18 timing config:load:flatten Completed in 6ms 19 timing npm:load:mkdirpcache Completed in 0ms 20 timing npm:load:mkdirplogs Completed in 1ms 21 verbose title npm install react@latest react-dom@latest next@latest 22 verbose argv "install" "react@latest" "react-dom@latest" "next@latest" 23 timing npm:load:setTitle Completed in 5ms 24 timing npm:load:display Completed in 1ms 25 verbose logfile logs-max:10 dir:C:\Users\HP\AppData\Local\npm-cache\_logs\2024-01-26T03_19_04_697Z- 26 verbose logfile C:\Users\HP\AppData\Local\npm-cache\_logs\2024-01-26T03_19_04_697Z-debug-0.log 27 timing npm:load:logFile Completed in 1293ms 28 timing npm:load:timers Completed in 0ms 29 timing npm:load:configScope Completed in 0ms 30 timing npm:load Completed in 1417ms 31 timing arborist:ctor Completed in 2ms 32 silly logfile start cleaning logs, removing 2 files 33 timing idealTree Completed in 723ms 34 timing command:install Completed in 759ms 35 verbose stack JSONParseError: Unexpected end of JSON input while parsing empty string 35 verbose stack at C:\Users\HP\AppData\Roaming\npm\node_modules\npm\node_modules\read-package-json-fast\lib\index.js:7:61 35 verbose stack at async #initTree (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:280:21) 35 verbose stack at async Arborist.buildIdealTree (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:193:7) 35 verbose stack at async Promise.all (index 1) 35 verbose stack at async Arborist.reify (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\reify.js:159:5) 35 verbose stack at async Install.exec (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\lib\commands\install.js:152:5) 35 verbose stack at async module.exports (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\lib\cli-entry.js:61:5) 36 verbose cwd C:\WEBSITES\Projets de la premiere cliente\platformes vente places d'installation 37 verbose Windows_NT 10.0.19045 38 verbose node v18.18.0 39 verbose npm v10.2.1 40 error code EJSONPARSE 41 error path C:\WEBSITES\Projets de la premiere cliente\platformes vente places d'installation/package.json 42 error JSON.parse Unexpected end of JSON input while parsing empty string 43 error JSON.parse Failed to parse JSON data. 43 error JSON.parse Note: package.json must be actual JSON, not just JavaScript. 44 verbose exit 1 45 timing npm Completed in 4137ms 46 verbose unfinished npm timer reify 1706239147476 47 verbose unfinished npm timer reify:loadTrees 1706239147499 48 verbose unfinished npm timer idealTree:init 1706239147501 49 verbose code 1 50 error A complete log of this run can be found in: C:\Users\HP\AppData\Local\npm-cache\_logs\2024-01-26T03_19_04_697Z-debug-0.log
尝试的无效操作
执行npm clean cache后返回错误:
npm ERR! 从npm@5开始,npm缓存会自动修复损坏问题
npm ERR! 从缓存中提取的数据保证是有效的。如果你
npm ERR! 想确保所有内容一致,请使用npm cache verify
npm ERR! 这不太可能解决你遇到的问题!
npm ERR!
npm ERR! 另一方面,如果你正在调试安装程序的问题,
npm ERR! 或者依赖于写入空缓存时机的竞争条件,你可以使用npm install --cache /tmp/empty-cache来使用临时
npm ERR! 缓存,而不是清空实际缓存。
npm ERR!
npm ERR! 如果你确定要删除整个缓存,请使用--force参数重新运行此命令。
解决方案
核心原因
空的package.json不符合JSON格式要求,npm无法解析空文件。
修复步骤
- 删除当前空的
package.json文件 - 在项目根目录执行初始化命令生成合法的
package.json:
(npm init -y-y参数会自动填充默认配置,无需手动确认) - 重新执行安装命令:
npm install react@latest react-dom@latest next@latest
可选验证步骤
如果担心缓存问题,执行缓存验证命令:
npm cache verify
内容的提问来源于stack exchange,提问作者Rovanel KIB

