Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 5 - Uncaught ReferenceError: process is not defined

Tags:

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. Now I'm getting this error, other answers haven't helped so much. crypto-browserify is trying to access process.browser. Can anyone shed more light? I was told by cli to install stream-browserify too so i did.

React v17, Babel 7.12.9, webpack 5.6.0

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

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(),   ], }); 
like image 747
pacifica94 Avatar asked Nov 26 '20 08:11

pacifica94


People also ask

How do you fix process is not defined react?

To solve the "Uncaught ReferenceError: process is not defined" in React, open your terminal in your project's root directory and update the version of your react-scripts package by running npm install react-scripts@latest and re-install your dependencies if necessary.

What is a polyfill webpack?

A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.

What are webpack plugins?

A webpack plugin is a JavaScript object that has an apply method. This apply method is called by the webpack compiler, giving access to the entire compilation lifecycle.


1 Answers

In webpack 5 automatic node.js polyfills are removed. 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. 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. 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. Next add alias for the lib in your webpack config.

For ex.

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

Update: This can also be done using ProvidePlugin

package.json

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

webpack.config.js

module.exports = {   ...   plugins: [       new webpack.ProvidePlugin({              process: 'process/browser',       }),   ], } 
like image 165
abhishek khandait Avatar answered Sep 27 '22 22:09

abhishek khandait