我创建了默认的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天前遇到了这个问题,目前仍在处理。


当前回答

“快速修复”的建议对我很有效。cd到你新建的App目录;您可能需要sudo这个安装,然后运行以下:

运行: NPM install -g NPM -check-updates

然后: 中央大学- u

然后: npm安装

然后: npm开始

其他回答

Try:

npm create react-app --template typescript foo --use-npm

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

请随意改正。

最初,假设这是我的包的脚本部分。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
...

你可以试试其中的一种:

1. 降级到Node.js v16。

您可以从Node.js的网站重新安装当前的LTS版本。 你也可以使用nvm。对于Windows,请使用vvm - Windows。

2. 启用旧的OpenSSL提供程序。

在类unix (Linux, macOS, Git bash等)上:

export NODE_OPTIONS=--openssl-legacy-provider

在Windows命令提示符中:

set NODE_OPTIONS=--openssl-legacy-provider

PowerShell:

$env:NODE_OPTIONS = "--openssl-legacy-provider"

参考

这个答案是一个立即的OpenSSL系统级解决方案,而不涉及以前工作的构建配置。

在理想的情况下,你有时间升级和迁移不安全的构建依赖项,并确保你没有破坏应用程序中的其他东西(或者完全避免webpack,或者在我的例子中从vue-cli迁移到使用esbuild的vite)。

相反,你“应该”(a)告诉webpack使用一个更新的哈希函数,或者(b)用npm审计减轻违规包。


系统级OpenSSL解决方案

最快的解决方法是通过在系统范围的OpenSSL配置中启用“遗留的”加密提供者来临时重新启用MD4。

这是非常不安全和笨拙的。然后,您应该禁用遗留的加密方法。

(不幸的是,下面的测试只适用于Linux)。

Backup your existing OpenSSL configuration: sudo cp /etc/ssl/openssl.cnf /etc/ssl/openssl.cnf.BAK Append (or uncomment) the following configuration to enable the legacy "providers" (as OpenSSL calls them). You probably want to sudo vim /etc/ssl/openssl.cnf or similar. [provider_sect] default = default_sect legacy = legacy_sect [default_sect] activate = 1 [legacy_sect] activate = 1 Rerun your node script as before. Disable the legacy providers afterwards. sudo mv -f /etc/ssl/openssl.cnf.BAK /etc/ssl/openssl.cnf

这个解决方案来自于一个类似问题的答案。


深层原因是什么?

Node在*nix系统上使用OpenSSL作为哈希函数和加密。最新版本的OpenSSL默认禁用MD4——这将破坏任何以前使用MD4的工作程序。考虑到这一点,任何认为使用MD4进行文件哈希是一个“好主意”的npm包现在都被破坏了——即使MD4自1996年以来就被RSA实验室认为是破坏的!MD4也在2011年被RFC 6150“正式”降级为过时。

在Windows上,通过卸载openssl 1.1.1,安装openssl 3(如果你使用chocolatey: choco install openssl.light),并将Angular更新到最新版本(目前为14),解决了上述webpack配置问题。

当然,正如大多数答案所建议的那样,降级Node并不是一个正确的方法。