我有一个Webpack构建过程突然崩溃的问题,导致以下错误…

<s> [webpack.Progress] 10% building 0/1 entries 0/0 dependencies 0/0 modules
node:internal/crypto/hash:67
  this[kHandle] = new _Hash(algorithm, xofLen);
                  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:67:19)
    at Object.createHash (node:crypto:130:10)
    at BulkUpdateDecorator.hashFactory (/app/node_modules/webpack/lib/util/createHash.js:155:18)
    at BulkUpdateDecorator.update (/app/node_modules/webpack/lib/util/createHash.js:46:50)
    at OriginalSource.updateHash (/app/node_modules/webpack-sources/lib/OriginalSource.js:131:8)
    at NormalModule._initBuildHash (/app/node_modules/webpack/lib/NormalModule.js:888:17)
    at handleParseResult (/app/node_modules/webpack/lib/NormalModule.js:954:10)
    at /app/node_modules/webpack/lib/NormalModule.js:1048:4
    at processResult (/app/node_modules/webpack/lib/NormalModule.js:763:11)
    at /app/node_modules/webpack/lib/NormalModule.js:827:5 {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}
command terminated with exit code 1

我尝试谷歌ERR_OSSL_EVP_UNSUPPORTED webpack,几乎没有任何有用的结果,但它确实突出了使用OpenSSL提供的MD4来生成哈希的问题(显然已弃用?)

webpack.config.js代码如下:

const path = require('path');
const webpack = require('webpack');

/*
 * SplitChunksPlugin is enabled by default and replaced
 * deprecated CommonsChunkPlugin. It automatically identifies modules which
 * should be splitted of chunk by heuristics using module duplication count and
 * module category (i. e. node_modules). And splits the chunks…
 *
 * It is safe to remove "splitChunks" from the generated configuration
 * and was added as an educational example.
 *
 * https://webpack.js.org/plugins/split-chunks-plugin/
 *
 */

/*
 * We've enabled TerserPlugin for you! This minifies your app
 * in order to load faster and run less javascript.
 *
 * https://github.com/webpack-contrib/terser-webpack-plugin
 *
 */

const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
    mode: 'development',
    entry: './src/js/scripts.js',

    output: {
        path: path.resolve(__dirname, 'js'),
        filename: 'scripts.js'
    },

    devtool: 'source-map',

    plugins: [new webpack.ProgressPlugin()],

    module: {
        rules: []
    },

    optimization: {
        minimizer: [new TerserPlugin()],

        splitChunks: {
            cacheGroups: {
                vendors: {
                    priority: -10,
                    test: /[\\/]node_modules[\\/]/
                }
            },

            chunks: 'async',
            minChunks: 1,
            minSize: 30000,
            name: 'true'
        }
    }
};

我如何改变哈希算法使用的Webpack其他的东西?


当前回答

我使用Laravel Mix (Webpack)遇到了这个问题,并能够在文件包中修复它。通过在脚本的开头添加NODE_OPTIONS=——openssl-legacy-provider(在Jan的回答中引用):

package.json:

{
  "private": true,
  "scripts": {
    "production": "cross-env NODE_ENV=production NODE_OPTIONS=--openssl-legacy-provider  node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
  "dependencies": {
    ...
  }
}

其他回答

这不是我的答案,但我发现这个变通/黑客/解决我的问题代码检入GitHub项目…在这里查看bug评论。

在更新npm install后,我遇到了ERR_OSSL_EVP_UNSUPPORTED。

我将以下内容添加到node_modules\react-scripts\config\webpack.config.js中

const crypto = require("crypto");
const crypto_orig_createHash = crypto.createHash;
crypto.createHash = algorithm => crypto_orig_createHash(algorithm == "md4" ? "sha256" : algorithm);

我尝试了Ryan Brownell的解决方案,最终出现了一个不同的错误,但这是有效的……

这意味着你拥有最新的Node.js版本。如果你正在使用它的Docker,那么你需要改变图像从

FROM node

to

FROM node:14

如果您正在使用Webpack v5.54 +, Ryan Brownell的答案是理想的解决方案。

如果您使用的是较旧版本的Webpack,您仍然可以通过将哈希函数更改为未弃用的哈希函数来解决这个问题。(它默认为古老的md4, OpenSSL已经删除了对md4的支持,这是错误的根本原因。)所支持的算法是crypt . createhash所支持的任何算法。以使用SHA-256为例:

module.exports = {
    output: {
        hashFunction: "sha256"
    }
};

最后,如果您无法更改Webpack配置(例如,如果它是正在运行Webpack的传递依赖项),您可以启用OpenSSL的遗留提供程序在Webpack构建期间临时启用MD4。这是最后的手段。用以下内容创建一个文件openssl.cnf…

openssl_conf = openssl_init

[openssl_init]
providers = provider_sect

[provider_sect]
default = default_sect
legacy = legacy_sect

[default_sect]
activate = 1

[legacy_sect]
activate = 1

然后在运行Webpack时将环境变量OPENSSL_CONF设置为该文件的路径。

我也遇到过同样的挑战,但你只需要把Node.js降级到16.13版本,一切就都可以正常工作了。下载LTS,而不是当前的下载。

我在用Next.js开发的项目中遇到了同样的问题。对于解决方案,我按照下面的方法运行项目,并解决了问题。

cross-env NODE_OPTIONS='--openssl-legacy-provider' next dev