npm ERR! unable to get local issuer certificate [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 23, 2023
4 min

banner

# npm ERR! unable to get local issuer certificate [Solved]

Disable the strict-ssl key to resolve the error "npm ERR! unable to get local issuer certificate".

When the strict-ssl key is set to false, NPM doesn't perform SSL key validation when making requests to the registry via https.

The article addresses the following errors:

  • npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  • npm ERR! unable to get local issuer certificate
  • gyp ERR! stack Error: unable to get local issuer certificate

The error occurs when you run the npm install command and try to download a package from a server with an HTTPS connection using a self-signed or an invalid certificate.

The easiest way to solve the error is to set the strict-ssl key to false.

shell
npm config set strict-ssl false npm config get strict-ssl

npm config set strict ssl false

If you use yarn, issue the following command instead.

shell
yarn config set strict-ssl false

yarn set strict ssl false

Try to rerun the npm install or yarn install command after disabling the strict-ssl key.

shell
# with NPM npm install # or with YARN yarn install

If you are running the npm install command from behind a proxy would like to clear the proxy or install modules from behind the proxy, check out the following article.

If you use a VPN, try turning off your VPN and rerunning the npm install command.

# Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to 0

If the error persists, try to set the NODE_TLS_REJECT_UNAUTHORIZED environment variable to 0.

If the environment variable is set to 0, certificate validation is disabled for TLS connections.

Note that setting the environment variable to 0 makes TLS and HTTPS insecure.

If you use bash or zsh, you would issue the following command.

shell
# for bash or ZSH export NODE_TLS_REJECT_UNAUTHORIZED=0

If you are on Windows and use CMD or PowerShell, issue one of the following commands.

shell
# for CMD set NODE_TLS_REJECT_UNAUTHORIZED=0 # for PowerShell $env:NODE_TLS_REJECT_UNAUTHORIZED="0"

Try to rerun the npm install command after making the change.

# Try updating your NPM registry

If the error persists, try to update your NPM registry.

shell
npm config set registry http://registry.npmjs.org/

Notice that the command uses the HTTP version of the NPM registry and not the HTTPS version.

This should resolve the error as you aren't trying to download a package from a server with an HTTPS connection using a self-signed or an invalid certificate.

Try to rerun the npm install command after making the change.

shell
# with NPM npm install # or with YARN yarn install

issue npm install command

If the error is resolved, you can try to flip your strict-ssl key to true and test if that works.

shell
npm config set strict-ssl true npm config get strict-ssl

You can always set the key back to false if the error occurs.

shell
npm config set strict-ssl false npm config get strict-ssl

# Try setting the --unsafe-perm flag when issuing the npm install command

If the error persists, try to set the --unsafe-perm flag when issuing the npm install command.

shell
npm install --unsafe-perm # or when installing a specific module npm install axios --unsafe-perm

npm install with unsafe perm flag

The --unsafe-perm flag forces npm to download the binary files of the packages to your project.

# Setting the NODE_EXTRA_CA_CERTS environment variable

If the error persists, you might be running into issues with your company's proxy.

If that's the case, you can:

  1. Find the path to the root CA certificate.
  2. Set the NODE_EXTRA_CA_CERTS environment variable.

The certificate file will likely have a .pem or .cer extension.

shell
# on macOS and Linux export NODE_EXTRA_CA_CERTS="/absolute/path/to/your/CA/cert.pem" # on Windows (CMD) set NODE_EXTRA_CA_CERTS="C:\Users\YOUR_USER\cert.pem"

You can also set the environment variable in your ~/.bashrc file or ~/.bash_profile if you are on macOS or Linux.

This way you won't have to rerun the command in every shell session.

shell
sudo gedit ~/.bashrc sudo gedit ~/.bash_profile sudo gedit ~/.zshrc

Make sure to source your profile file if end up setting the environment variable.

shell
source ~/.bashrc source ~/.bash_profile source ~/.zshrc

If you are on Windows, you can set the environment variable in your system's environment variables.

If you need to clear your proxy settings or install a module from behind a proxy, check out the following article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.