unable_to_get_issuer_cert_locally Error in Node JS

Resolving the ‘unable_to_get_issuer_cert_locally’ Error in Node.js

Understanding the ‘unable_to_get_issuer_cert_locally’ Error in Node.js

This error typically arises when Node.js is unable to verify an SSL certificate. The root of the problem lies in the absence of the issuer’s certificate in the local trust store. When Node.js tries to connect to an HTTPS server, it must validate the server’s SSL certificate. This process involves confirming the certificate’s chain of trust, where the Certificate Authority (CA) plays a crucial role. If the CA’s certificate is missing in the trust store, Node.js cannot validate the chain, resulting in the ‘unable_to_get_issuer_cert_locally’ error.

Effective Solutions to Resolve the Error

1. Disable Strict SSL Verification Temporarily

This is a quick fix, especially when you’re unable to obtain the SSL certificate or face issues even after adding it to the trust list. Use the command:

npm config set strict-ssl false

Remember, this is a temporary solution. Once the SSL issue is resolved, re-enable strict SSL verification using npm config set strict-ssl true to avoid security risks like Man-in-the-Middle attacks.

2. Switch to HTTP for NPM Registry

If the HTTPS version of the NPM registry is causing issues, switch to HTTP:

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

Note that this is primarily a workaround and may not be suitable for all situations.

3. Expand Node.js Trust Store

To include additional Root certificates in your trust store, use these commands based on your operating system:

  • MacOS/Linux:
  export NODE_EXTRA_CA_CERTS=path/to/my-certs.pem
  • Windows:
  set NODE_EXTRA_CA_CERTS=C:\\path\\to\\certificate.pem

4. Alter CAfile Settings

Change the CA file settings to prioritize a specific certificate:

npm config set cafile /path/to/root/certificate.pem

This method overrides the standard CA lookups used by NPM.

5. Disable Certificate Verification

For testing in non-production environments, you might disable SSL certificate verification:

export NODE_TLS_REJECT_UNAUTHORIZED=0

However, this is discouraged due to the significant security risks involved. Ensure to switch back to secure settings after testing.

Handling the Error Behind Proxies

If you’re behind a corporate proxy that replaces SSL certificates, you might face this error. In such cases, obtain the .pem certificate file from your network administrator or download it from a website that uses the certificate. Then, add this certificate to your trusted certificates list.

Conclusion

The ‘unable_to_get_issuer_cert_locally’ error in Node.js is a common SSL verification issue. This guide provides various strategies to address the problem, from temporarily bypassing strict SSL verification to adjusting the trust store and CAfile settings. While some methods, like disabling certificate verification, are effective, they pose significant security risks and should be used cautiously. If you encounter this error due to a proxy, coordinating with your network administrator for the appropriate certificate is essential. By understanding and applying these solutions, you can ensure a smoother and more secure Node.js development experience.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top