当创建一个新的Angular 5项目时:

节点版本号:8.9.2

NPM版本:5.5.1

我的命令是:

npm install -g @angular/cli

错误是:

npm ERR! **Unexpected end of JSON input while parsing near '...nt-webpack-plugin":"0'**
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Aashitec\AppData\Roaming\npm-cache\_logs\2017-12-06T13_10_10_729Z-debug.log

错误日志在这里


当前回答

你可以设置一个临时文件夹,而不是清除缓存:

npm install --cache /tmp/empty-cache

or

npm install --global --cache /tmp/empty-cache

从npm@5开始,npm缓存会从损坏问题中自我修复,并且从缓存中提取的数据保证是有效的。如果你想确保所有内容都是一致的,可以使用npm缓存验证。另一方面,如果你正在调试安装程序的问题,你可以使用npm install——cache /tmp/empty-cache来使用一个临时缓存,而不是破坏实际的缓存。

其他回答

如果npm cache clean——force不能解决这个问题,试着删除~/。npm目录(*nix/macOS)。这个目录存放node的缓存、锁、日志、全局包(除非你使用nvm)和通过npx安装的模块。

首先,备份当前的~/。npm目录:

mv ~/.npm ~/.npm-backup

现在再次尝试运行你的npm命令。这将创建一个新的~/。npm目录。如果问题已解决,您可以安全地删除备份。在这样做之前,您可能需要检查安装在~/中的全局包。所以你可以使用NPM I -g [package]重新安装它们。

rm -rf ~/.npm-backup

如果问题没有解决,您可以恢复备份:

rm -rf ~/.npm
mv ~/.npm-backup ~/.npm

小心那些rm命令,伙计们!

我用

npm cache clean --force

然后更新NPM

npm i npm@latest -g

然后通常使用NPM安装命令

npm install 

这条命令就解决了我的问题:

npm cache clean --force

此外,您还应该确保使用了正确的节点版本。

使用nvm管理节点版本:

nvm list; # check your local versions;
nvm install 10.10.0; # install a new remote version;
nvm alias default 10.10.0; # set the 10.10.0 as the default node version, but you have to restart the terminal to make it take effect;

对我来说,我不得不再次克隆我的分支,并进行npm安装

在我的例子中,我把网络换成了一个更强大的网络,npm安装工作得很好:

npm cache clean --force