解决启动Vue项目时遇到的 error:0308010C:digital envelope routines::unsupported 错误
问题描述
最近,在启动一个遗留前端(Vue)项目时,遇到了error:0308010C:digital envelope routines::unsupported
错误。
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:79:19)
at Object.createHash (node:crypto:139:10)
at D:\code\front\shucheng-admin-ui\node_modules\compression-webpack-plugin\dist\index.js:243:42
at CompressionPlugin.compress (D:\code\front\shucheng-admin-ui\node_modules\compression-webpack-plugin\dist\index.js:284:9)
at D:\code\front\shucheng-admin-ui\node_modules\compression-webpack-plugin\dist\index.js:305:12
at _next1 (eval at create (D:\code\front\shucheng-admin-ui\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:14:17)
at eval (eval at create (D:\code\front\shucheng-admin-ui\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:33:1)
at D:\code\front\shucheng-admin-ui\node_modules\copy-webpack-plugin\dist\index.js:91:9
这是因为Node.js v17
及以上版本默认使用 OpenSSL 3.x
,而 OpenSSL 3.x
移除了对一些旧加密算法的支持。因此,依赖这些旧算法的模块(如CompressionPlugin
)在运行时可能会抛出 error:0308010C:digital envelope routines::unsupported
错误。
解决方案(TL;DR)
方案一:使用 OpenSSL 旧版提供程序
通过在启动命令中设置 NODE_OPTIONS=--openssl-legacy-provider
,强制 Node.js 使用旧版的 OpenSSL 提供程序,从而支持被移除的旧算法。
具体操作
Windows 系统:
"scripts": {
"dev": "set NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service serve",
"build:prod": "set NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service build",
"build:stage": "set NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service build --mode staging"
}
Unix/Linux/Mac 系统:
"scripts": {
"dev": "NODE_OPTIONS=--openssl-legacy-provider vue-cli-service serve",
"build:prod": "NODE_OPTIONS=--openssl-legacy-provider vue-cli-service build",
"build:stage": "NODE_OPTIONS=--openssl-legacy-provider vue-cli-service build --mode staging"
}
方案二:降级 Node.js 版本
如果您不需要 Node.js v17 及以上版本的新功能,可以考虑降级到 Node.js v16 或更早的版本。这些版本默认使用 OpenSSL 1.x,通常不会遇到该问题。
方案三:更新相关依赖
检查是否有更新版本的 CompressionPlugin 或其他依赖库,新版本可能已经兼容 OpenSSL 3.x。