【node 运行项目报错】 使用node最新版本,运行旧项目报错

起因

node 升级到最新的版本v18之后运行之前的项目报错


```Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (D:\code\5\PC2\PC2\node_modules\webpack\lib\util\createHash.js:135:53)
    at NormalModule._initBuildHash (D:\code\5\PC2\PC2\node_modules\webpack\lib\NormalModule.js:417:16)
    at handleParseError (D:\code\5\PC2\PC2\node_modules\webpack\lib\NormalModule.js:471:10)
    at D:\code\5\PC2\PC2\node_modules\webpack\lib\NormalModule.js:503:5
    at D:\code\5\PC2\PC2\node_modules\webpack\lib\NormalModule.js:358:12
    at D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:373:3
    at iterateNormalLoaders (D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:214:10)
    at iterateNormalLoaders (D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:221:10)
    at D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:236:3
    at runSyncOrAsync (D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:130:11)
    at iterateNormalLoaders (D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:232:2)
    at Array. (D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:205:4)
    at Storage.finished (D:\code\5\PC2\PC2\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:55:16)
    at D:\code\5\PC2\PC2\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:91:9
node:internal/crypto/hash:71
  this[kHandle] = new _Hash(algorithm, xofLen);
                  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (D:\code\5\PC2\PC2\node_modules\webpack\lib\util\createHash.js:135:53)
    at NormalModule._initBuildHash (D:\code\5\PC2\PC2\node_modules\webpack\lib\NormalModule.js:417:16)
    at handleParseError (D:\code\5\PC2\PC2\node_modules\webpack\lib\NormalModule.js:471:10)
    at D:\code\5\PC2\PC2\node_modules\webpack\lib\NormalModule.js:503:5
    at D:\code\5\PC2\PC2\node_modules\webpack\lib\NormalModule.js:358:12
    at D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:373:3
    at iterateNormalLoaders (D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:214:10)
    at Array. (D:\code\5\PC2\PC2\node_modules\loader-runner\lib\LoaderRunner.js:205:4)
    at Storage.finished (D:\code\5\PC2\PC2\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:55:16)
    at D:\code\5\PC2\PC2\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:91:9
    at D:\code\5\PC2\PC2\node_modules\graceful-fs\graceful-fs.js:123:16
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3) {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

![在这里插入图片描述](https://img-blog.csdnimg.cn/3b1cff6ef88047b5b60ffdb4c172f064.png)

### 解决 
pakeage.json文件中,修改package.json,在相关构建命令之前加入SET NODE_OPTIONS=--openssl-legacy-provider,

“scripts”: {
“serve”: “SET NODE_OPTIONS=–openssl-legacy-provider && vue-cli-service serve”,
“build”: “SET NODE_OPTIONS=–openssl-legacy-provider && vue-cli-service build”
},

"scripts": {
   "serve": "SET NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service serve",
   "build": "SET NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service build"
},

再执行 npm run serve 就可以运行项目了,如果不行 新开一个窗口再执行一下npm run serve

原因

node.js 的版本问题

因为 node.js V17版本中最近发布的OpenSSL3.0, 而OpenSSL3.0对允许算法和密钥大小增加了严格的限制,可能会对生态系统造成一些影响。故此以前的项目在升级 nodejs 版本后会报错。

你可能感兴趣的:(前端,javascript,开发语言)