我有一个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": {
    ...
  }
}

其他回答

我使用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

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

在Node.js 17.0.0的发布说明中提到了这个错误,并给出了建议的解决方案:

如果你在使用Node.js 17的应用程序中遇到ERR_OSSL_EVP_UNSUPPORTED错误,很可能是你的应用程序或你正在使用的模块试图使用OpenSSL 3.0默认不再允许的算法或密钥大小。添加了命令行选项——openssl-legacy-provider,用于恢复到遗留提供程序,作为这些严格限制的临时解决方案。