node.js 错误!代码 UNABLE_TO_GET_ISSUER_CERT_LOCALLY

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45884752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:27:34  来源:igfitidea点击:

npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY

node.jsreactjsnpmcreate-react-app

提问by Dinesh

I am trying all the ways of creating react application. I have tried with maven and now i am trying with crate-react-app build system from Facebook Incubators.

我正在尝试创建反应应用程序的所有方法。我已经尝试过 maven,现在我正在尝试使用来自 Facebook 孵化器的 crate-react-app 构建系统。

When i tried to run the command create-react-app my-appin npm environment, it worked on my personal system with no issues. But when i tried the same command in my work environment, i have encountered with this error on command prompt

当我尝试create-react-app my-app在 npm 环境中运行该命令时,它在我的个人系统上运行没有问题。但是当我在我的工作环境中尝试相同的命令时,我在命令提示符下遇到了这个错误

npm ERR! node v6.10.2
npm ERR! npm  v3.10.10
npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY

npm ERR! unable to get local issuer certificate
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

回答by Dinesh

A quick solution from the internet search was npm config set strict-ssl false, luckily it worked. But as a part of my work environment, I am restricted to set the strict-ssl flag to false.

互联网搜索的一个快速解决方案是npm config set strict-ssl false,幸运的是它有效。但作对我来说有效环境的一部分,我只能将 strict-ssl 标志设置为 false。

Later I found a safe and working solution,

后来我找到了一个安全有效的解决方案,

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

this worked perfectly and I got a success message Happy Hacking!by not setting the strict-ssl flag to false.

这工作得很好,我Happy Hacking!通过不将 strict-ssl 标志设置为 false得到了一条成功消息。

回答by Jordan

what may be happening is your company decrypts certain traffic and re-encrypts it with their certificate (which you probably already have in your keychain or trusted root certificates)

可能发生的情况是您的公司解密某些流量并使用他们的证书(您可能已经在您的钥匙串或受信任的根证书中)重新加密它

if you're using node 7 or later I've found this fix to be compatible with node and node-gyp (for Windows you'll need to do this differently, but you basically just need to add this environment variable):

如果您使用的是 node 7 或更高版本,我发现此修复程序与 node 和 node-gyp 兼容(对于 Windows,您需要以不同的方式执行此操作,但您基本上只需要添加此环境变量):

export NODE_EXTRA_CA_CERTS="absolute_path_to_your_certificates.pem"(in Windows you may need to remove the quotes - see comments)

export NODE_EXTRA_CA_CERTS="absolute_path_to_your_certificates.pem"(在 Windows 中,您可能需要删除引号 - 请参阅注释)

the pem file can have multiple certificates: https://nodejs.org/api/cli.html#cli_node_extra_ca_certs_file

pem 文件可以有多个证书:https: //nodejs.org/api/cli.html#cli_node_extra_ca_certs_file

make sure your certificates are in proper pem format (you need real line breaks not literal \n)

确保您的证书采用正确的 pem 格式(您需要真正的换行符而不是文字\n

I couldn't seem to get it to work with relative paths (.or ~)

我似乎无法让它与相对路径(.~)一起工作

This fix basically tells npm and node-gyp to use the check against the regular CAs, but also allow this certificate when it comes across it

此修复基本上告诉 npm 和 node-gyp 使用针对常规 CA 的检查,但在遇到此证书时也允许此证书

Ideally you would be able to use your system's trusted certificates, but unfortunately this is not the case.

理想情况下,您可以使用系统的可信证书,但不幸的是,情况并非如此。

回答by Robert Dundon

Changing the NPM repo URL to HTTP works as a quick-fix, but I wanted to use HTTPS.

将 NPM 存储库 URL 更改为 HTTP 是一种快速修复,但我想使用 HTTPS。

In my case, the proxy at my employer (ZScaler) was causing issues (as it acts as a MITM, causing certification verification issues)

就我而言,我雇主 (ZScaler) 的代理造成了问题(因为它充当 MITM,导致认证验证问题)

I forgot I found a scriptthat helps with this and Git (for cloning GitHub repos via HTTPS had the same issue) and forked it for my use

我忘了我找到了一个有助于解决这个问题的脚本,而 Git(通过 HTTPS 克隆 GitHub 存储库也有同样的问题)并将其分叉供我使用

Basically, it does the following for git:

基本上,它对 git 执行以下操作:

git config --global http.proxy http://gateway.zscaler.net:80/
git config --system http.proxy http://gateway.zscaler.net:80/

and for Node, it adds proxy=http://gateway.zscaler.net:80/to the end of c:\Users\$USERNAME\npm\.npmrc

对于 Node,它添加proxy=http://gateway.zscaler.net:80/到末尾c:\Users\$USERNAME\npm\.npmrc

That solved the issue for me.

那为我解决了这个问题。

回答by Prashanth Keshanna

Trust me, this will work for you:

相信我,这对你有用:

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

回答by RyanDay

After trying out every solution I could find:

在尝试了我能找到的所有解决方案后:

  • Turning off strict ssl: npm config set strict-ssl=false
  • Changing the registry to http instead of https: npm config set registry http://registry.npmjs.org/
  • Changing my cafile setting: npm config set cafile /path/to/your/cert.pem
  • Stop rejecting unknown CAs: set NODE_TLS_REJECT_UNAUTHORIZED=0
  • 关闭严格的 ssl: npm config set strict-ssl=false
  • 将注册表更改为 http 而不是 https: npm config set registry http://registry.npmjs.org/
  • 更改我的咖啡馆设置: npm config set cafile /path/to/your/cert.pem
  • 停止拒绝未知 CA: set NODE_TLS_REJECT_UNAUTHORIZED=0

The solution that seems to be working the best for me now is to use the NODE_EXTRA_CA_CERTSenvironment variable which extends the existing CAs rather than replacing them with the cafile option in your .npmrc file. You can set it by entering this in your terminal: NODE_EXTRA_CA_CERTS=path/to/your/cert.pem

现在似乎对我来说效果最好的解决方案是使用NODE_EXTRA_CA_CERTS环境变量来扩展现有的 CA,而不是用 .npmrc 文件中的 cafile 选项替换它们。您可以通过在终端中输入以下内容来设置它:NODE_EXTRA_CA_CERTS=path/to/your/cert.pem

Of course, setting this variable every time can be annoying, so I added it to my bash profile so that it will be set every time I open terminal. If you don't already have a ~/.bash_profilefile, create one. Then at the end of that file add export NODE_EXTRA_CA_CERTS=path/to/your/cert.pem. Then, remove the cafile setting in your .npmrc.

当然,每次都设置这个变量会很烦人,所以我将它添加到我的 bash 配置文件中,以便每次打开终端时都会设置它。如果您还没有~/.bash_profile文件,请创建一个。然后在该文件的末尾添加export NODE_EXTRA_CA_CERTS=path/to/your/cert.pem. 然后,删除 .npmrc 中的 cafile 设置。

回答by Jason Geiger

Had the same error. Looks like it is related to SSL certificates. If you are using NPM for public packages (don't need the security of HTTPS) you can turn off strict SSL key validation with the following command.

有同样的错误。看起来它与 SSL 证书有关。如果您将 NPM 用于公共包(不需要 HTTPS 的安全性),您可以使用以下命令关闭严格的 SSL 密钥验证。

This might be the simplest fix if you're just looking to install a few publicly available packages one time.

如果您只想一次性安装一些公开可用的软件包,这可能是最简单的修复方法。

npm config set strict-ssl=false

回答by drussey

I had this error when I tried to update npm, but had a really old version (1.3.6 !) installed from yum in AWS Linux. I was able to manually install a newer npm version and everything was remedied.

我在尝试更新 npm 时遇到了这个错误,但是在 AWS Linux 中从 yum 安装了一个非常旧的版本(1.3.6 !)。我能够手动安装更新的 npm 版本,一切都得到了补救。

回答by Mohd Shariq

The below code worked for me perfectly here make http only instead https

下面的代码对我来说很完美,这里只使用 http 而不是 https

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