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


当前回答

我也有这个问题。我不小心运行的是最新的Node.js(写这篇文章时是17.0),而不是我想安装的LTS版本(14.18)。将我的Node.js安装降级到LTS版本为我解决了这个问题。

其他回答

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

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

我也有这个问题。我不小心运行的是最新的Node.js(写这篇文章时是17.0),而不是我想安装的LTS版本(14.18)。将我的Node.js安装降级到LTS版本为我解决了这个问题。

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

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

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

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

FROM node

to

FROM node:14