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(), ], });
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.
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.
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.
In webpack 5 automatic node.js polyfills are removed. In the migration docs it is mention that
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', }), ], }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With