我创建了默认的IntelliJ IDEA React项目,并得到了这个:

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:67:19)
    at Object.createHash (node:crypto:130:10)
    at module.exports (/Users/user/Programming Documents/WebServer/untitled/node_modules/webpack/lib/util/createHash.js:135:53)
    at NormalModule._initBuildHash (/Users/user/Programming Documents/WebServer/untitled/node_modules/webpack/lib/NormalModule.js:417:16)
    at handleParseError (/Users/user/Programming Documents/WebServer/untitled/node_modules/webpack/lib/NormalModule.js:471:10)
    at /Users/user/Programming Documents/WebServer/untitled/node_modules/webpack/lib/NormalModule.js:503:5
    at /Users/user/Programming Documents/WebServer/untitled/node_modules/webpack/lib/NormalModule.js:358:12
    at /Users/user/Programming Documents/WebServer/untitled/node_modules/loader-runner/lib/LoaderRunner.js:373:3
    at iterateNormalLoaders (/Users/user/Programming Documents/WebServer/untitled/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
    at iterateNormalLoaders (/Users/user/Programming Documents/WebServer/untitled/node_modules/loader-runner/lib/LoaderRunner.js:221:10)
/Users/user/Programming Documents/WebServer/untitled/node_modules/react-scripts/scripts/start.js:19
  throw err;
  ^

这似乎是最近才出现的问题——webpack在4天前遇到了这个问题,目前仍在处理。


当前回答

我在Docker构建中遇到了这个问题,我在Docker文件中添加了这一行:

RUN export NODE_OPTIONS=--openssl-legacy-provider && yarn build && yarn install --production --ignore-scripts --prefer-offline

对于本地开发,在package.json文件中添加交换机。

其他回答

听起来很简单,如果可行的话,升级包中的所有依赖项。json到最新的(只要在npm中输入名称并使用建议的版本),也使用node的最新LTS版本。

我以前遇到过问题,即使将我的项目迁移到使用yarn,我也能够最终解决这个问题,不需要用ssl黑客来妥协安全性

它是Node.js版本。

我在Node.js 17上有这个错误,但当我使用nvm将我的Node.js版本切换到旧版本(16)时,它是好的。

在使用VueJS时遇到了这个问题。

使用-openssl-legacy-provider的一个缺点是旧版本的Node不支持它。当提供此标志时,旧版本的Node根本不运行。 但我仍然希望与Node v16及更早版本兼容。

VueJS使用md4算法来生成散列(实际上它是WebPack的底层)。出于这些目的,md5可以很容易地取代Md4。所使用的算法类型,在大多数地方都是硬编码的,所以没有办法配置另一种算法。

所以我想出了另一个变通办法。从crypto模块拦截原始createHash()调用并将其替换为修改后的版本的函数。这是vue.config.js文件的开头:

const crypto = require('crypto');

/**
 * md4 algorithm is not available anymore in NodeJS 17+ (because of lib SSL 3).
 * In that case, silently replace md4 by md5 algorithm.
 */
try {
  crypto.createHash('md4');
} catch (e) {
  console.warn('Crypto "md4" is not supported anymore by this Node version');
  const origCreateHash = crypto.createHash;
  crypto.createHash = (alg, opts) => {
    return origCreateHash(alg === 'md4' ? 'md5' : alg, opts);
  };
}

如果遇到此错误,并且不想更改主配置,则可以使用以下方法进行简单的修复。不过,我不确定这是否被推荐为一种好的做法。

请随意改正。

最初,假设这是我的包的脚本部分。json文件:

...
"version": "1.0.0",
  "scripts": {
    ...
    "build": "npm run build:test-app:testing",
    "build:test-app:testing": "ng build test-app --deploy-url  https://test-app.com/ --configuration=test-config",
    ...
  },
  "private": true,
...

为了使用这个导出NODE_OPTIONS=——openssl-legacy-provider,您可以执行以下操作:

"version": "1.0.0",
  "scripts": {
....
    "build": "NODE_OPTIONS=--openssl-legacy-provider npm run build:test-app:testing”,
    "build:test-app:testing": "NODE_OPTIONS=--openssl-legacy-provider ng build test-app --deploy-url  https://test-app.com/ --configuration=test-config"
...
  },
  "private": true,

注意构建脚本。我添加了一个选项:NODE_OPTIONS=——openssl-legacy-provider

这有助于在Node.js version 17中解决此错误。

对于那些可以灵活更改构建系统的Node.js版本的人,只需切换到低于17的版本,例如,版本16。

对于Docker,最初使用this的用例,它总是提取最新版本:

...
FROM node:alpine
...

你可以切换到如下内容:

...
FROM node:16-alpine3.12
...

临时解决方案如下。实际的解决方案是升级到Webpack 5。

2022年12月更新

这对我来说很管用(降级到Node.js 16):

nvm install 16 --lts
nvm use 16

使用Node.js版本管理器(Windows)。