When you install npm packages using npm install
command, sometimes there will be an npm error when parsing the JSON response from the registry.
An example of the error is as follows:
npm ERR! registry error parsing json
npm ERR! Darwin 20.6.0
npm ERR! node v4.9.1
npm ERR! npm v2.15.11
npm ERR! Unexpected token <
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /Users/nsebhastian/npm-debug.log
This error is caused by an invalid JSON response from the registry.
However, finding what causes npm to receive an invalid JSON response is a bit complicated to do.
Here are four things you can try to resolve the issue:
- Set registry to npmjs
- Clean npm cache and node modules
- Check your internet proxy
- Upgrade your npm to the latest LTS version
This tutorial will help you learn how to do the above fixes.
Set registry to npmjs
You need to make sure that your registry is pointed to https://registry.npmjs.org to try and fix this error.
You can check your registry by running the get registry
command as follows:
$ npm config get registry
https://npm.strongloop.com
As of this writing, any public alternatives to the official registry from npmjs.org have expired.
If you have any other entry than npmjs.org, then you need to change the registry using the set registry
command as follows:
npm config set registry https://registry.npmjs.org
After that, try running npm install
and see if it works now.
Clear node_modules folder and npm cache files
The first thing you can try to fix the error is to clear the project module dependencies. This includes the node_modules
folder and package-lock.json
file.
You can do this by running the following command from your project root directory:
rm -rf node_modules
rm package-lock.json
Once you clear the project module dependencies, clean your npm cache with the following command:
$ npm cache clean --force
Once done, run npm install
again and see if it works.
Check your internet proxy
You need to check your npm error output and see if there’s a line saying Authentication Required as shown below:
npm ERR! Authentication Required
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /Users/nsebhastian/npm-debug.log
The error highlighted above usually indicates that you are accessing the internet using a proxy. You need to authenticate npm so that it can access the internet using your proxy.
Set the proxy in the npm config with the following commands:
npm config set proxy 'username:[email protected]'
npm config set https-proxy 'username:[email protected]'
Keep in mind that this will expose your proxy username and password to anyone who can access your npm installation. The credentials are written in the .npmrc
file.
You can use Fiddler as a bridge between npm and your proxy to avoid writing your username and password in the config file.
You can see a guide here: Using Fiddler as proxy for npm
If you are using NTLM for the proxy, you can also use CNTLM to set up a localhost proxy that authenticates your npm install
command.
Here’s a guide to setting up CNTLM: CNTLM as proxy for npm
However you want to authenticate your npm install
internet access, please make sure that the proxy is the cause of the error.
You can always delete your proxy credentials with npm config delete
:
npm config delete proxy
npm config delete https-proxy
The above commands should remove your proxy credentials from the .npmrc
file.
Upgrade your npm to the latest LTS version
If you never upgrade your Node.js installation, then it’s possible that you have a very old version of npm installed on your computer.
It’s best to check your Node.js and npm version and make sure that you’re at least 2 versions down from the latest version.
As of this writing, here are the LTS versions available in Node:
lts/erbium -> v12.22.12
lts/fermium -> v14.19.2
lts/gallium -> v16.15.0
The latest version is lts/gallium
on v16.15.0
so you want to make sure that you’re at least using Node v12.22.12
. Node versions below lts/erbium
no longer receive support and bug fixes.
You can learn more about Node.js releases here.
Check your Node and npm versions by running the following commands from the terminal:
npm -v
node -v
If you want to upgrade just npm, then you can follow this guide to use the latest stable version of npm.
A very old version of npm may not be able to download and install packages from the registry, so you need to upgrade it.
Conclusion
The npm registry error in parsing JSON occurs when an invalid JSON entry is given to npm.
While npm expects a valid JSON text, an HTML file can be given as a response to the npm install
command.
The four methods above should help you to resolve the issue, but if you still see the same error, you might want to post the detail of your error to StackOverflow so that others can help you resolve it.
Thanks for reading! I hope this tutorial has helped you 🙏