安装 reactjs 时出错:解析附近时 JSON 输入错误意外结束

Error installing reactjs: Error unexpected end of JSON input while parsing near

我尝试在终端中使用两个不同的命令通过节点安装 reactjs,使用 1) npx create-react-app my-app 和 2) npx create-react-app app

但两者都产生了如下所示的相同错误:

Installing packages. This might take a couple of minutes.

Installing react, react-dom, and react-scripts with cra-template...

npm ERR! Unexpected end of JSON input while parsing near '....1","object-hash":"^1'

npm ERR! A complete log of this run can be found in:

npm ERR! C:\Users\David\AppData\Roaming\npm-cache_logs20-02-12T03_53_37_836Z-debug.log

Aborting installation.

npm install --save --save-exact --loglevel error react react-dom react-scripts cra-template has failed.

我尝试卸载并重新安装节点,还尝试安装不同的旧版本的 reactjs,但仍然出现相同的错误。此外,我尝试清除节点缓存,然后通过 npm cache clear --forcenpm cache verify 再次验证它,但仍然没有任何运气。任何帮助将不胜感激。

更详细的日志显示在底部:

203 silly saveTree `-- react@16.12.0

204 verbose stack SyntaxError: Unexpected end of JSON input while parsing near '....1","object-hash":"^1'

204 verbose stack at JSON.parse ()

204 verbose stack at parseJson (C:\Program Files\nodejs\node_modules\npm\node_modules\json-parse-better-errors\index.js:7:17)

204 verbose stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-fetch-npm\src\body.js:96:50

204 verbose stack at processTicksAndRejections (internal/process/task_queues.js:97:5)

205 verbose cwd C:\Users\David\Desktop\React\my-app

206 verbose Windows_NT 10.0.17763

207 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "install" "--save" "--save-exact" "--loglevel" "error" "react" "react-dom" "react-scripts" "cra-template"

208 verbose node v13.8.0

209 verbose npm v6.13.6

210 error Unexpected end of JSON input while parsing near '....1","object-hash":"^1'

211 verbose exit [ 1, true ]

使用节点包管理器安装 yarn 解决了我的问题。打开命令提示符并输入:

npm install -g yarn

在您的终端中尝试运行:

npm cache clean --force

然后重做你正在做的事情

解决方案:

npm cache clean --force

然后再次尝试创建您的应用程序(在此处创建 React 应用程序时)或安装您要安装的应用程序。

create-react-app myproject

(创建 React 应用程序)[在不同操作中可能出现的相同 npm 问题]

npm install -g @angular/cli@latest

(正在安装 angular cli(或安装任何其他软件))

它会起作用。

解释:

这是一个与 npm 相关的问题,它与缓存损坏有关。尽管在较新版本的 npm 中他们实现了自我修复,这通常可以保证不会损坏,但似乎效率不高。 强制清理缓存可解决问题。

解析其中一个具有 json 格式的缓存文件时发生错误。缓存位于 ~/.npm/_cacache(在 linux)和 %AppData%/npm-cache(windows)。对于我当前的 npm 版本,当我检查时,有三个目录。

如果勾选第一个或者第二个,结构如下

并且每个缓存文件都有一个 json 格式(以及被解析的内容)

这里有一个很好的 link 来自文档:https://docs.npmjs.com/cli/cache

[更新] 此外,如果碰巧这没有解决问题,您可以在此处查看此答案 它显示了如何更改 npm 注册表,这可能会有所帮助。检查看看。

我遇到了同样的问题但是 npm cache clean --force 对我不起作用。

我所做的是 清理我的电脑

现在可以使用我的清洁器 (CCleaner) 和 npm 安装了。我这样做是因为在我卸载 node.js 后出现问题,因为我想在我的 PC (https://docs.microsoft.com/en-us/windows/nodejs/setup-on-windows) 上安装 nvm-windows,但是在我这样做之后我认为一些文件和文件夹在我使用 nvm 安装节点后留下了冲突,因此 npm install 不起作用。所以我认为清洁我的电脑可以解决问题,结果确实如此。

这些只是我的想法。希望能帮到人。

问题解决使用:

npm 缓存清理 --force :/

sudo npm install -g create-react-app 为我工作

这是由于 Yarn 中的硬缓存造成的。我通过删除 node_modules 文件夹并再次 运行

解决了这个问题
yarn install 

生成了新的 node_modules 文件夹,问题已解决。