简体   繁体   English

Webpack 5 - 未捕获的引用错误:未定义进程

[英]Webpack 5 - Uncaught ReferenceError: process is not defined

Webpack newbie here, I was told by webpack cli that I needed to provide an alias for crypto as webpack no longer includes default node libraries. Webpack 新手在这里,webpack cli 告诉我,我需要为加密提供别名,因为 webpack 不再包含默认节点库。 Now I'm getting this error, other answers haven't helped so much.现在我遇到了这个错误,其他答案并没有太大帮助。 crypto-browserify is trying to access process.browser . crypto-browserify正在尝试访问process.browser Can anyone shed more light?谁能阐明更多? I was told by cli to install stream-browserify too so i did. cli 告诉我也安装stream-browserify ,所以我这样做了。

React v17, Babel 7.12.9, webpack 5.6.0反应 v17,通天塔 7.12.9,webpack 5.6.0

webpack.common.js webpack.common.js

const paths = require('./paths');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const dotenv = require('dotenv-webpack');

module.exports = {
  entry: [paths.src + '/index.js'],
  output: {
    path: paths.build,
    filename: '[name].bundle.js',
    publicPath: '/',
  },
  plugins: [
    new dotenv(),
    new CleanWebpackPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: paths.public,
          to: 'assets',
          globOptions: {
            ignore: ['*.DS_Store'],
          },
        },
      ],
    }),
    new HtmlWebpackPlugin({
      title: 'Webpack Boilerplate',
      // favicon: paths.src + '/images/favicon.png',
      template: paths.src + '/template.html',
      filename: 'index.html',
    }),
  ],
  resolve: {
    fallback: {
      crypto: require.resolve('crypto-browserify'),
      stream: require.resolve('stream-browserify'),
    },
  },
  module: {
    rules: [
      // javascript
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      // images
      {
        test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
        type: 'asset/resource',
      },
      // Fonts and SVGs
      {
        test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
        type: 'asset/inline',
      },
      // CSS, PostCSS, and Sass
      {
        test: /\.(scss|css)$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              esModule: true,
              sourceMap: true,
              importLoaders: 1,
              modules: {
                auto: true,
                namedExport: true,
              },
            },
          },
          { loader: 'postcss-loader', options: { sourceMap: true } },
          { loader: 'sass-loader', options: { sourceMap: true } },
        ],
      },
    ],
  },
};

webpack.dev.js webpack.dev.js

const webpack = require('webpack');
const { merge } = require('webpack-merge');
const common = require('./webpack.common');

module.exports = merge(common, {
  mode: 'development',

  // Control how source maps are generated
  devtool: 'inline-source-map',

  // Spin up a server for quick development
  devServer: {
    historyApiFallback: true,
    contentBase: paths.build,
    open: true,
    compress: true,
    hot: true,
    port: 8080,
  },

  plugins: [
    // Only update what has changed on hot reload
    new webpack.HotModuleReplacementPlugin(),
  ],
});

In webpack 5 automatic node.js polyfills are removed.在 webpack 5 自动 node.js 填充被删除。 In the migration docs it is mention that在迁移文档中提到

  • Try to use frontend-compatible modules whenever possible.尽可能尝试使用与前端兼容的模块。
  • It's possible to manually add a polyfill for a node.js core module.可以为 node.js 核心模块手动添加 polyfill。 An error message will give a hint on how to achieve that.一条错误消息将提示如何实现这一点。
  • Package authors: Use the browser field in package.json to make a package frontend-compatible. Package 作者:使用 package.json 中的浏览器字段来制作与 ZEFE90A8E604A7C840E88D03 兼容的前端。 Provide alternative implementations/dependencies for the browser.为浏览器提供替代实现/依赖项。

See this issue.看到这个问题。

Now you can refer this PR and check the libs that were removed and install them.现在您可以参考此PR并检查已删除的库并安装它们。 Next add alias for the lib in your webpack config.接下来在 webpack 配置中为该库添加别名

For ex.例如。

resolve: {
    alias: {
       process: "process/browser"
    } 
 }

Update: This can also be done using ProvidePlugin更新:这也可以使用ProvidePlugin来完成

package.json package.json

"devDependencies": {
   ...
   "process": "0.11.10",
}

webpack.config.js webpack.config.js

module.exports = {
  ...
  plugins: [
      new webpack.ProvidePlugin({
             process: 'process/browser',
      }),
  ],
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM