我有一个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其他的东西?


当前回答

Webpack v5.54.0+提供了一个不依赖OpenSSL的哈希算法。

要使用这个依赖于npm提供的依赖项而不是操作系统提供的依赖项的哈希函数,请修改webpack.config.cjs输出键以包含hashFunction: "xxhash64"选项。

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

其他回答

试着升级你的Webpack版本到5.62.2。

我可以通过以下方法修复它:

export NODE_OPTIONS=--openssl-legacy-provider

sachaw对Node.js v17.0.0的注释-在开发模式#30078启动项目时出错

但是他们说他们已经修复了这个问题:ijjk对Node.js v17.0.0的注释-在开发模式#30078启动项目时出错:

您好,这已在v11.1.3-canary中更新。89的Next.js,请更新并尝试!

对我来说,它只适用于上面的注释。

我还想指出,npm run start与-openssl-legacy-provider一起工作,但npm run dev不能。

似乎有一个补丁: Node.js 17:数字信封例程::不支持#14532

我个人把级别降到了16级。

这不是我的答案,但我发现这个变通/黑客/解决我的问题代码检入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的解决方案,最终出现了一个不同的错误,但这是有效的……

Webpack v5.54.0+提供了一个不依赖OpenSSL的哈希算法。

要使用这个依赖于npm提供的依赖项而不是操作系统提供的依赖项的哈希函数,请修改webpack.config.cjs输出键以包含hashFunction: "xxhash64"选项。

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

我使用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": {
    ...
  }
}