无法通过npm安装任何包,遇ERR_OSSL_UNSUPPORTED错误求助
问题描述
我执行任何npm包安装命令(比如安装typescript)时,都会出现以下错误和警告:
npm WARN config key
keyandcertare no longer used for most registry operations.
npm WARN config Use registry scopedkeyfileandcertfileinstead.
npm WARN config Example:
npm WARN config //other-registry.tld/:keyfile=/path/to/key.pem
npm WARN config //other-registry.tld/:certfile=/path/to/cert.crt
npm WARN config keykeyandcertare no longer used for most registry operations.
npm WARN config Use registry scopedkeyfileandcertfileinstead.
npm WARN config Example:
npm WARN config //other-registry.tld/:keyfile=/path/to/key.pem
npm WARN config //other-registry.tld/:certfile=/path/to/cert.crt
npm ERR! code ERR_OSSL_UNSUPPORTED
npm ERR! error:1E08010C:DECODER routines::unsupported
解决方法
1. 修复ERR_OSSL_UNSUPPORTED错误
这个错误大多是Node.js使用的SSL版本不兼容或OpenSSL设置问题,可按以下方式解决:
- 单命令临时修复:
在执行npm安装时加上环境变量参数:NODE_OPTIONS=--openssl-legacy-provider npm install typescript - Windows永久修复:
- 打开「系统属性」→「高级」→「环境变量」
- 在「用户变量」里点击「新建」
- 变量名填
NODE_OPTIONS,变量值填--openssl-legacy-provider - 重启终端后再执行npm命令
- macOS/Linux永久修复:
把环境变量添加到shell配置文件(比如~/.bashrc或~/.zshrc):echo 'export NODE_OPTIONS="--openssl-legacy-provider"' >> ~/.zshrc # 如果是bash,就改成 echo 'export NODE_OPTIONS="--openssl-legacy-provider"' >> ~/.bashrc source ~/.zshrc
2. 消除过时配置警告
警告是因为npm配置里的key和cert参数已经过时,需要更新:
- 先查看当前npm配置:
npm config list - 删除旧的
key和cert配置项:npm config delete key npm config delete cert - 如果需要给特定私有仓库配置证书,改用作用域的
keyfile和certfile(替换成你的仓库地址和证书路径):npm config set //你的仓库域名/:keyfile=/证书文件路径/key.pem npm config set //你的仓库域名/:certfile=/证书文件路径/cert.crt
内容的提问来源于stack exchange,提问作者AKASH KUMAR

