报错信息::95% emitting CompressionPlugin ERROR Error: error:0308010C:digital envelope routines::unsupported
报错原因:
主要是因为 nodeJs V17 版本发布了 OpenSSL3.0 对算法和秘钥大小增加了更为严格的限制,nodeJs v17 之前版本没影响,但 V17 和之后版本会出现这个错误。 我的node版本是v18+
博主完全报错信息(仅参考):
INFO Starting development server...
95% emitting CompressionPlugin ERROR Error: error:0308010C:digital envelope routines::unsupported
Error: error:0308010C:digital envelope routines::unsupported
at new Hash (node:internal/crypto/hash:69:19)
at Object.createHash (node:crypto:133:10)
at CompressionPlugin.taskGenerator (E:\code\xxx\node_modules\compression-webpack-plugin\dist\index.js:163:38)
at taskGenerator.next (<anonymous>)
at E:\code\xxx\node_modules\compression-webpack-plugin\dist\index.js:216:49
at CompressionPlugin.runTasks (E:\code\xxx\node_modules\compression-webpack-plugin\dist\index.js:236:9)
at E:\code\xxx\node_modules\compression-webpack-plugin\dist\index.js:270:18
at _next1 (eval at create (E:\code\xxx\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:14:17)
at eval (eval at create (E:\code\xxx\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:33:1)
at E:\code\xxx\node_modules\copy-webpack-plugin\dist\index.js:91:9
Process finished with exit code 1
解决方案:
方案1:
Windows:
在 Windows PowerShell 中设置环境变量后,您可以重新启动开发服务器。如果您使用的是命令提示符(CMD),请使用以下命令:
-
输入以下命令来设置
NODE_OPTIONS
环境变量:$env:NODE_OPTIONS="--openssl-legacy-provider"
-
然后,您可以用以下命令检查环境变量是否设置成功:
echo $env:NODE_OPTIONS
然后,重新启动您的 Node.js 应用程序。
但是使用 PowerShell 设置环境变量的方法 $env:NODE_OPTIONS="--openssl-legacy-provider"
会创建一个临时的环境变量,它只在当前的 PowerShell 会话中有效。如果你关闭 PowerShell 窗口,这个变量就会丢失。
该方法只在当前会话框内有效,以下是运行成功图:
如果需要完全配置,需要根据执行一下操作:
要创建一个永久的环境变量,你需要通过系统的环境变量设置来添加它。以下是在 Windows 中设置永久环境变量的步骤:
- 按下
Windows
键,然后输入环境变量
,点击编辑系统环境变量
。- 在系统属性窗口中,点击
环境变量
按钮。- 在
环境变量
窗口中,你可以选择添加到用户变量或系统变量。用户变量只适用于当前用户,而系统变量适用于所有用户。根据需要选择新建
。- 在变量名字段中输入
NODE_OPTIONS
,在变量值字段中输入--openssl-legacy-provider
。- 点击确定保存你的设置。
完成这些步骤后,你可能需要重新启动你的计算机,或者至少重新启动任何打开的命令行或 IDE,以便更改生效
Linux & Mac OS(未验证):
遇到的错误与Node.js及其构建时使用的OpenSSL版本有关。这通常在Node.js 17及以上版本中看到,可以通过设置环境变量来解决,该变量强制Node.js使用旧版的加密操作提供程序。
以下是用伪代码表示的逐步计划:
- 设置
NODE_OPTIONS
环境变量以使用旧版提供程序。 - 重启开发服务器。
现在,设置环境变量的实际代码:
export NODE_OPTIONS=--openssl-legacy-provider
设置此环境变量后,尝试再次启动开发服务器。
方案2:(仅限 windows):
在项目中 package.json 的 scripts 中新增 SET NODE_OPTIONS=--openssl-legacy-provider
文件图片:
添加前:
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
添加后
"scripts": {
"serve": "set NODE_OPTIONS=--openssl-legacy-provider & vue-cli-service serve",
"build": "set NODE_OPTIONS=--openssl-legacy-provider & vue-cli-service build",
"lint": "vue-cli-service lint"
},
我是用的是windows上的解决办法,已测试。本文章仅作记录。
参考链接:
VUE解决Error: error:0308010C:digital envelope routines::unsupported的四种解决方案 - 前端哥