[Fixed] npm ERR! registry error parsing json

Troubleshooting and resolutions for the error npm ERR! registry error parsing json

Mar 18, 2023 | Read time 9 minutes

🔔 Table of contents

Introduction

If you are running npm install and getting the error of npm ERR! registry error parsing json - this would mean that you are not getting a success response from the NPM registry.

A more verbose error would look like the following:

npm ERR! registry error parsing json
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "U:\\Tools\\nodejs\\\\node.exe" "U:\\Tools\\nodejs\\node_modules\\npm\\bin\\npm-cli.js
" "-g" "install" "grunt-cli" "karma" "bower"
npm ERR! node v0.12.4
npm ERR! npm  v2.10.1

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>

What does registry error parsing json mean?

When you get this type of error when doing a npm install there could be a range of causes.

The error can look something like the following:

Error: Invalid JSON

npm ERR! SyntaxError: Unexpected token <

npm ERR! registry error parsing json

There are a few causes, but the top ones that can be the culprit includes:

  • There is a temporary npm registry glitch, or corrupted local server cache. Run npm cache clean and/or try again later.
  • package.json is not correct or valid JSON
  • Working behind the corporate proxy that is spitting out errors or returning HTML responses

Steps to fix this error

  1. Check that you have internet connectivity and can connect to the NPM registry
  2. Make sure to have pointed to the correct registry url
  3. Clear the NPM cache
  4. Check and update your proxy settings
  5. Remove the node_modules folder and package-lock.json file and install again
  6. Upgrade the Node and NPM versions
  7. Update the timeout configurations
  8. Check the package.json file is correct

1. Check that you have internet connectivity and can connect to the NPM registry

Before doing anything else, we first need to check connectivity.

Additionally, on some cases there could be a temporary npm registry glitch. So we need to make sure that the problem is not with our local machine!

Follow the below steps to check if we are able to connect to the internet. Open up your command line/ terminal cmd, and run the following commands:

  • ping 8.8.8.8 - this is Google’s DNS
  • Check that the DNS resolver is working working: ping www.google.com
  • Check the NPM registry URL: ping registry.npmjs.org
  • Open up the browser and see if you can load https://registry.npmjs.org

2. Make sure to have pointed to the correct registry url

The NPM registry is the location where NPM looks for packages when you use the npm install command.

When we first install NPM the public registry is set to HTTPS (https://registry.npmjs.org), however we can change this with the npm config command.

To set the NPM registry, follow these steps:

  1. Open a terminal or command prompt window.

  2. We can check our current NPM registry location as follows:

npm config get registry

  1. If you got the wrong registry url, enter the following command to set the registry to the public NPM registry:

npm config set registry https://registry.npmjs.org/

3. Clear the NPM cache

On occasions, it could be that the local cache that is on your machine is corrupt and this will lead to the error

To fix this, just open up the terminal and run:

npm cache clean --force

This command makes sure we don’t have random modules from the cache.

NPM usually stores modules in your local project folder and also a “cached” folder.

For Linux and OSX systems - this will be on ~/.npm.

With Windows systems, this folder is %AppData%/npm-cache.

Caching helps with performance and saves you bandwidth - eg the next time you install a similar module it will go off to the cache instead of downloading it!

4. Check and update your proxy settings

After the previous steps were successful, run the following commands to clear your current proxy settings (they are not working anyway).

npm config rm proxy

npm config rm https-proxy

Set new proxy settings

After we have cleared the existing proxy settings, we first need to make sure that we set the registry:

npm config set registry https://registry.npmjs.org/

Now set the new proxy settings with the following commands. Replace the proxyname with your corporate proxy URL.

npm config set proxy http://username:password@proxyname:8080

npm config set https-proxy http://username:password@proxyname:8080

Keep in mind that when you are using username and password, they need to be encoded. For example, if your password is: Welcome@12# then it will be like Welcome%4012%23.

Additionally, with your username, you may need to also include the domain name + username aswell.

For example, lets say we work at a company with domain BIGCORP and your username is johnnyweekend with password Welcome@12#, then your NPM proxy config might look something like this:

npm config set proxy http://bigcorp\\jonnyweekend:Welcome%4012%23@bigcorpproxy:8080

Tip: Check your corporate proxy settings and make sure that they are not blocking NPM registry

Check with your corporate network team that the proxy is not blocking the following URL: https://registry.npmjs.org

5. Remove the node_modules folder and package-lock.json file and install again

One option that worked well for NPM issues for me is to nuke the whole project - removing node_modules and reinstall everything.

To do this we can use the steps, open up the terminal and make sure you are in the root directory of the project:

  1. npm install -g npm@latest to update npm because it is sometimes buggy.
  2. rm -rf node_modules to remove the existing modules.
  3. Run npm cache clean --force to make sure we don’t have random modules from the cache.
  4. Execute npm install to re-install the project dependencies.

6. Upgrade the Node and NPM versions

One good tool to use to manage Node versions is NVM!

To work with NVM and manage your Node versions we can do the following:

  1. For windows, we can go to the binary and install NVM located here:

https://github.com/coreybutler/nvm-windows/tags

For linux distros we can do the following:

sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
nvm install node 
  1. Uninstall all versions of node with the following command:

nvm uninstall <node version>

  1. Reinstall needed node version:

nvm install <node version>

To get the latest node version we can do nvm install latest

  1. Use node version just installed:

nvm use <node version>

In our case, we can use the command nvm use latest

7. Update the timeout configurations

One thing that we can try when you have a slow connection is to increase the timeout values:

npm config set fetch-retry-mintimeout 20000
npm config set fetch-retry-maxtimeout 120000

8. Check the package.json file is correct

A common reason why we get the error parsing json error is that our package.json is not correct.

To fix this, just run it through a JSON validator and make sure that it is correct.

Consider the below package.json file.

{
  "name": "@myscope/myrepo",
  "description": "some package",
  "version": "3.0.0",
  "homepage": "https://github.com/...",
  "author": {
    "name": "Lorem Ipsum",
    "email": "lorem"
  },
  ...
  "scripts": {
    "test": "grunt test"
  },
  "devDependencies": {},
  "dependencies": {
    "@myscope/otherprivaterepo": ">=0.1.2",
    "async": "0.1.15",
    "aws-sdk": "^2.6.9",
    "mysql": "github:mysqljs/mysql" 
    "redis": "^2.6.2",
    "request": "^2.75.0"
  }
}

We can see that the following line is not valid:

"mysql": "github:mysqljs/mysql"

This is because it is missing a comma at the end of the line.

Summary

In this post, we looked at the issue of npm ERR! registry error parsing json. This error means that when we are getting a response from the NPM registry, it is not return a successfull JSON response.

There are a few reasons why this can error can occur - ranging from a intermittent issue with the NPM registry itself (https://registry.npmjs.org), the package.json is not valid and working behind a corporate proxy causing responses to return HTML responses.

To fix this error, we can follow steps such as - checking your internet connectivity, configure the proxy settings for NPM, checking that you have the right NPM registry url and Node versions and finally verify that your package.json is valid.

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube