399

When creating a new Angular 5 project:

node version: 8.9.2

npm version: 5.5.1

My Command is:

npm install -g @angular/cli

The Error is:

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

The error log is here

Elizabeth
  • 176
  • 2
  • 10
kathir
  • 4,255
  • 2
  • 15
  • 25
  • 44
    "npm cache clean --force" without quotes might be the answer to your question. – Bimal Dec 17 '19 at 07:28
  • 11
    Worked for me, said it hoped I knew what I was doing. I did not. – carny666 May 21 '20 at 18:11
  • `sudo npm cache clean --force` `sudo npm cache verify` `sudo npm i npm@latest -g` These commands worked for me – Zohab Ali Jun 19 '20 at 15:05
  • 1
    @tonix thanks bro, your solution is working for me also, npm ERR! Unexpected end of JSON input while parsing near '...lFicG0K5VKDuUbV0\n4AU' (npm install --save firebase-admin) – kusama Oct 26 '20 at 06:00
  • 1
    It is not working for me, I tried multiple times, if I give cache clean next time I get error like npm ERR! sha512-I4kM.... – Vasanth Dec 11 '20 at 07:37
  • I had to try using different terminals, I used git bash, command prompt, and windows terminal all running as administrator while always running "npm cache clean --force" before my "npm install" command would work correctly. – Zach Pedigo Jan 12 '23 at 15:23

38 Answers38

892

This solved it for me:

Open Windows Powershell as admin

npm cache clean --force
npm install -g @angular/cli

https://devblogs.microsoft.com/premier-developer/getting-started-with-node-js-angular-and-visual-studio-code/

Qonvex620
  • 3,819
  • 1
  • 8
  • 15
mcemmy
  • 9,232
  • 1
  • 11
  • 4
  • repeatedly running npm cache clean --force and npm install -g @angular/cli@latest solved my issues. running npm cache clean did not help – Squapl Recipes Feb 26 '19 at 15:16
  • It worked for me, but not before showing the following warning: "I sure hope you know what you are doing", I do not get why... – perodriguezl Apr 16 '19 at 13:45
  • If your buildserver produces this, you need to run the cache clean with your buildserver or under the user the buildserver runs with. The cache seems to be user specific – MichaelD May 15 '19 at 07:57
  • worked for me but after running the command like 6 times after each `npm install` command – Keshav Sharma Jul 22 '19 at 09:08
  • 4
    That works but when I run it under `C:\Users\[user-name]\AppData\Roaming\npm` main directory – Market Jul 31 '19 at 14:07
  • Try deleting package-lock.json and then execute the above commands – Alok Dec 20 '19 at 11:20
  • 2
    it works but it just frustrating every npm relate problem pretty much all come down to delete node_modules/ or clean cache – NamNamNam Apr 21 '20 at 05:00
  • 4
    What's super annoying for me is that when you're installing hundreds of packages and one of them is failing it's impossible to tell which one. These npm error logs are less than useless. – Eugene Kuzmenko Sep 13 '20 at 16:44
  • I ended up here with the same error creating a react project using "npx create-react-app". Clearing the npm cache worked for me too, just in case anyone else is in the same boat. – bobwah Oct 28 '20 at 08:54
192

Solution:

npm cache clean --force

then try again to create your app (here when creating a react app) or install what you were about to install.

create-react-app myproject

(creating react app)[same npm problem that can occur in different operation]

npm install -g @angular/cli@latest

(installing angular cli (or installing anything else))

It will work.

explanation:

That's a problem related to npm, it's about the cache that get corrupt. Even though in newer versions of npm they implemented self healing, which normally guarantee no corruption, but it seem it's not that efficient. enter image description here Forcing clean cache resolve the problem.

The error happen when parsing one of the cache files, which have a json format. The cache is found at ~/.npm/_cacache (in linux) and %AppData%/npm-cache (windows). For my current npm version and when i checked, there was three directories.
enter image description here

if you check the first or the second, the structure is as follow enter image description here

And each cache file have a json format (and that what get parsed) enter image description here

Here a good link from the doc: https://docs.npmjs.com/cli/cache

[Update] Also if it happen that this didn't solve it, you may check this answer here https://stackoverflow.com/a/50191315/7668448 it show how you can change the npm registry, that can be helpful. Check and see.

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
  • 3
    Well explained answer sir thanks a lot I was wondering why this had happening and i suspected that my files got corrupted because of a bad internet connection which caused the cache file to be corrupted since the download wasn't complete could this be true – Charles Dec 16 '18 at 09:52
  • 1
    Yes and an unsure no. Yes that was an issue in the past. As per this issue thread https://github.com/npm/npm/issues/4652. But they working on it. Now i'm unsure, maybe it is solved. It all depend on the implementation and how npm manage all the steps from fetching to caching to installing. I don't have time to go through npm internal right now. But i'm elaborating around this question, doing some research. I will write an article, and update the answer later when it's done. Know that they may have other reason to the error then a cache corruption, – Mohamed Allal Dec 16 '18 at 12:04
  • 2
    see https://docs.npmjs.com/common-errors#invalid-json. For registry problem there is verdaccio that one can try https://github.com/verdaccio/verdaccio. advised as per those threads https://github.com/npm/npm/issues/21181, https://github.com/npm/npm/issues/21112. And here a thread about the cache and how they were moving https://github.com/npm/npm/issues/4652. Another article which is history now https://www.infoworld.com/article/3198746/javascript/npm-5-reworks-the-cache-for-speed-but-will-break-with-older-code.html. I let you know when i finish my research and writing an article. – Mohamed Allal Dec 16 '18 at 12:14
  • Here an interesting thread: Here an interesting thread https://github.com/yarnpkg/yarn/issues/393 – Mohamed Allal Dec 16 '18 at 12:41
  • 1
    The issue had bothered me for a while and back with consistent error. Thanks to this post, I deleted the two folders and now things work for me properly! Thanks – Jim Yu Apr 21 '20 at 00:16
  • glad to hear that! This comment can be valuable too for others! Thanks too – Mohamed Allal Apr 21 '20 at 01:04
90

If

npm cache clean --force

doesn't work. try

npm cache clean --force
npm update
MD SHAYON
  • 7,001
  • 45
  • 38
  • 2
    As of May 2020, this is the only solution that actually works perfectly as intended for the workflow error – Akhil May 04 '20 at 16:26
  • 1
    It depends. Npm claims a 'self healing' cache, but as so often, they presume that the user has an impeccable internet connection. Here in the PH this is not the case. I ran `npm outdated` and then internet dropped. `npm clean --force` solved it. I can imagine if you were running updates/installs, your solution is needed. – Dimitri Mostrey Jun 29 '20 at 09:08
  • 1
    Solved my issue too. In my specific case, it didn't work with `sudo`, but it worked wonders without it. :) – SrAxi Jun 22 '21 at 12:42
31

Npm uses Cache for downloading new packages for you. You need to clear your npm cache. use following command to clean :

npm cache clean --force

then normally use your npm install command e.g.

npm install -g @angular/cli
Amit Gaikwad
  • 516
  • 4
  • 12
19

I solve that with

npm cache clean --force

then update npm

npm i npm@latest -g

then normally use your npm install command

npm install 
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Hasibul-
  • 1,192
  • 1
  • 9
  • 18
14

delete npm and npm-cache folders in C:\Users\admin\AppData\Roaming\ (windows) then execute cmd

npm cache clear --force

npm cache verify

update npm to latest version

npm i -g npm

then create your project 1)Angular

npm i -g @angular/cli@latest

ng new HelloWorld

2)React

npm i -g create-react-app

create-react-app react-app

  • I cannot execute npm i -g npm, getting error npm ERR! Unexpected end of JSON input while parsing near '...am","fstream-npm","gi' – Vasanth Aug 11 '20 at 09:18
12

None of the 30 answers here worked for me. I needed to:

  1. delete node_modules and package-lock.json
  2. npm cache clean --force
  3. npm install -g @angular/cli
  4. npm i --package-lock-only
  5. npm ci

Phew!

Christopher Grigg
  • 2,238
  • 27
  • 33
10

this solved it npm cache clean --force

WasiF
  • 26,101
  • 16
  • 120
  • 128
BIS Tech
  • 17,000
  • 12
  • 99
  • 148
8

Simple solutions:

npm cache clean --force
npm install 
Shashwat Gupta
  • 5,071
  • 41
  • 33
6

npm cache clean --force worked for me

Error Resolved :

$ npm install -g gulp npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5 npm ERR! Unexpected end of JSON input while parsing near '.../RGs88STtAtiMP3tCiNdU'

npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\admin\AppData\Roaming\npm-cache_logs\2018-11-20T07_38_56_733Z-debug.log

WasiF
  • 26,101
  • 16
  • 120
  • 128
user10680309
  • 61
  • 1
  • 1
6

This command alone solved my problem:

npm cache clean --force

Also you should make sure you are using the correct version of node.

Using nvm to manage the node version:

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;
Hearen
  • 7,420
  • 4
  • 53
  • 63
6

I use Windows and removed all the files that were listed below and my problem was solved C:\Users{{your-username}}\AppData\Roaming\npm-cache

milad karimi
  • 61
  • 1
  • 5
4

Solution

npm cache clean --force

For Windows : go to C:\Users\username\AppData\Roaming\npm-cache
Delete all files and run

npm install && npm start
Nick
  • 138,499
  • 22
  • 57
  • 95
mahendra
  • 87
  • 4
3

I resolved my problem with this one liner

npm cache clean --force

Note: since its a clean install, I had no concerns emptying npm cache.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
pasignature
  • 577
  • 1
  • 6
  • 13
3

I solved that by first cleaning the cache using

npm cache clean --force

Then

npm install -g @angular/cli
Desire Kaleba
  • 1,264
  • 11
  • 11
3

My case - Windows 7 (had nothing better at the needed moment). Helped me the following:

  1. Deleting everything from C:\Users\username\AppData\Roaming\npm-cache

AND

  1. Deleting package-lock.json
ysvet
  • 163
  • 1
  • 1
  • 10
2

ERROR: npm ERR! Unexpected end of JSON input while parsing near '...ore-js":"3.0.0-beta.1

This occurs when installing the expo CLI globally, This works for me!

npm cache clean --force
2

I got error (file already exists --force to overwrite) after running the following code:

npm cache clean --force
npm install -g @angular/cli

I solved it using :

npm i -g --force npm

Make sure to run the first commands to flush the cache of npm.

Rijo
  • 2,568
  • 1
  • 22
  • 30
1

It's error from your npm....

So unistall node and install it again.

It works....

PS: After installing node again, install angular cli globally.

npm install -g @angular/cli@latest
raghu rkm
  • 19
  • 1
1

I solve that with

First delete package-lock.json

npm cache clean --force

then update npm

npm i npm@latest -g

then use npm install command

npm install 
Muhammad Tahir
  • 2,351
  • 29
  • 25
1

If npm cache clean --force doesn't resolve the issue, try deleting the ~/.npm directory (*nix/macOS). This is the directory where node stores its cache, locks, logs, global packages (unless you're using nvm), and modules installed via npx.

First, backup your current ~/.npm directory:

mv ~/.npm ~/.npm-backup

Now try running your npm command again. This will create a new ~/.npm directory. If the issue is resolved, you can safely remove you backup. Before doing so, you may want to review the global packages installed in your ~/.npm-backup directory so you can reinstall them using npm i -g [package].

rm -rf ~/.npm-backup

If the issue is not resolved, you can restore your backup:

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

Careful with those rm commands, folks!

vlad-ardelean
  • 7,480
  • 15
  • 80
  • 124
John Hildenbiddle
  • 3,115
  • 2
  • 19
  • 14
1

If you looking for npm install and getting same kind of error

Delete package-lock.json and npm cache clean --force and try

Overflowrun
  • 541
  • 3
  • 18
1

In my case I changed the network to a much stronger one and npm install worked perfectly:

npm cache clean --force
Sercan
  • 4,739
  • 3
  • 17
  • 36
Afuye Olawale
  • 141
  • 1
  • 8
0

Instead of clearing the cache you can set a temporary folder:

npm install --cache /tmp/empty-cache

or

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

As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to be valid. If you want to make sure everything is consistent, use npm cache verify instead. On the other hand, if you're debugging an issue with the installer, you can use npm install --cache /tmp/empty-cache to use a temporary cache instead of nuking the actual one.

Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
0

For me I had to clone my branch again and do npm install

mtchuente
  • 334
  • 5
  • 9
0

You can use yarn package manager instead of npm.

It solved this problem for me

0

I've faced this issue and I tried all the answers on Stackoverflow but not worked for me, finally I found solution for this issue:

  • First of all you have to uninstall nodejs and remove all the files related him
  • Go to "Register Editor" and search for all "nodejs", "node.js" and delete them
  • Restart your computer
  • And reinstall nodejs then install angular.

It's worked for me

Mohamad Alhamid
  • 346
  • 4
  • 15
0

These commands worked for me

sudo npm cache clean --force

sudo npm cache verify

sudo npm i npm@latest -g
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Zohab Ali
  • 8,426
  • 4
  • 55
  • 63
0

Try setting

npm config set strict-ssl false

and then try running,

npm install -g @angular/cli

0

This happens something due to cache or outdated version of Node Package Manager i just updated my NPM it works fine here is command to update Npm to latest version

npm i npm@latest -g

after updating NPM run your desired command

Hanzla Habib
  • 3,457
  • 25
  • 25
0

Even if npm cache clean --force

Try to execute the installing cmd in admin folder.
i.e C:\Users\admin
This worked out for me.

LST
  • 43
  • 7
0

Just faced the issue of Unexpected end of JSON input while parsing near.. while adding the 'radium' package in my React App. As a matter of fact, I am facing this issue even when trying to update the NPM to the latest version.

Anyways, NPM didn't work after clearing the cache and it also won't update to the latest version right now but adding the package via Yarn did the trick for me.

So, if you are in a hurry to solve this issue but you are not able to, then give yarn a try instead of npm.

Happy Coding!

Update

After trying few things finally sudo npm cache clean --force worked for me.

This can be a temporary glitch in your network or with something else in the npm registry.

Rupak
  • 17
  • 1
  • 1
  • 5
0
npm uninstall -g angular-cli

npm cache clear --force

OR

Delete this folder directory (In Windows)

rmdir C:\Users\<ProfileName>\AppData\Roaming\npm
rmdir C:\Users\<ProfileName>\AppData\Roaming\npm-cache
Parthasarathy S
  • 197
  • 1
  • 6
0

Firstly uninstall the existing npm package: npm uninstall -g create-react-app (-g if you installed globally) Secondly: npm cache clean --force Thirdly: npm install -g create-react-app@latest and create-react-app my-app again.

0

This bug may be caused by many different things.Maybe your global packages are corrupted in some way, I suggest doing the following.

1 Install Node Version Manager (or NVM for Windows) and install Node version 10.16.1. This is enough for angular 9 and from my experience, it is the most stable version for development. This should also change your NPM version so that may also help.

2 Uninstall global angular package npm uninstall -g @angular/cli

3 Force clear cache npm cache clean --force

4 You can also try clearing your %temp% and %roaming% AppData/npm-cache

5 install latest angular package npm install -g @angular/cli@<your-version>

6 if this error still occurs, consider using another shell.

Source- https://www.codegrepper.com/code-examples/javascript/npm+ERR%21+Unexpected+end+of+JSON+input+while+parsing+near+%27...babel-plugin-istanbul%27+npm+ERR%21+A+complete+log+of+this+run+can+be+found+in%3A+npm+ERR%21+C%3A%5CUsers%5Csapho%5CAppData%5CRoaming%5Cnpm-cache%5C_logs%5C2020-08-26T20_37_45_303Z-debug.log+Aborting+installation.

Utkarsh Singh
  • 347
  • 3
  • 10
0

Open windows PowerShell or CMD as admin

npm cache clean --force
npm install -g @angular/cli

If this didn't work try the following

npm cache clean --force
npm update
npm install -g @angular/cli
loopassembly
  • 2,653
  • 1
  • 15
  • 22
0

Well everyone is saying

npm cache clean --force

But if that doesn't work then follow these steps.

  1. Reinstall npm: Reinstalling npm can sometimes resolve conflicts and other issues with npm itself. To reinstall npm, follow these steps:

Uninstall npm:

npm uninstall -g npm

Delete the npm cache folder:

npm cache clean --force

Install new LTS node that is supported by the packages. You may receive an error when you run the below to reinstall npm if the node version is not supported. This is the kind of error you could get:

npm ERR! notsup Required: {"node":"^14.17.0 || ^16.13.0 || >=18.0.0"}

npm ERR! notsup Actual: {"npm":"9.4.0","node":"v16.0.0"}

So you could run, like in my case

nvm i 16.13.0

Where 'i' is short for 'install', same as

nvm install 16.13.0

Then Reinstall npm:

npm install -g npm

If the above doesnt work...you should try the next steps

  1. Delete the node_modules folder and package-lock.json file: The node_modules folder and package-lock.json file can sometimes become corrupted, leading to errors when installing packages. To resolve this, you can try deleting these files and then reinstalling your packages:

Delete the node_modules folder:

rm -rf node_modules

Delete the package-lock.json file:

rm package-lock.json

Reinstall your packages:

npm install

ELSE

  1. Use a different version of Node.js: If you are encountering errors while using a specific version of Node.js, you can try switching to a different version to see if the issue is resolved. You can use a version manager such as nvm to manage different versions of Node.js on your system.

  2. Check for malware or virus: If you are still encountering issues, there may be malware or a virus on your system that is interfering with npm. Check your system for malware or viruses and remove any that are found.

Whizfactor
  • 41
  • 1
  • 1
  • 4
-2

After this npm cache clean --force

may be you can hangup or waiting for further execution

npm WARN using --force I sure hope you know what you are doing.

So you can also use this one. This resolve my issue.

npm install --cache /tmp/empty-cache