Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Polyfill node core modules in webpack 5

webpack 5 no longer do auto-polyfilling for node core modules. How to fix it please? PS: I'm a beginner in development so solution must be well described fo me.

errors

like image 835
Saber Avatar asked Oct 27 '20 15:10

Saber


People also ask

What is node polyfill?

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.

Can you use webpack for node?

Most developers use Webpack via the Webpack CLI, but Webpack also has an excellent Node. js API. That means you can run Webpack from your Node. js scripts, like an Express server, without a task runner.

Where is webpack config js file?

The webpack configuration file webpack. config. js is the file that contains all the configuration, plugins, loaders, etc. to build the JavaScript part of the NativeScript application. The file is located at the root of the NativeScript application.

Do you need node for webpack?

js. Webpack runs on Node. js, a JavaScript runtime that can be used in computers and servers outside a browser environment.


2 Answers

I was also getting these error's when upgrading from webpack v4 to v5. Resolved by making the following changes to webpack.config.js

added resolve.fallback property

removed node property

{ resolve: {   modules: [...],   fallback: {     "fs": false,     "tls": false,     "net": false,     "path": false,     "zlib": false,     "http": false,     "https": false,     "stream": false,     "crypto": false,     "crypto-browserify": require.resolve('crypto-browserify'), //if you want to use this module also don't forget npm i crypto-browserify    }  }, entry: [...], output: {...}, module: {   rules: [...] }, plugins: [...], optimization: {   minimizer: [...], }, // node: { //   fs: 'empty', //   net: 'empty', //   tls: 'empty' // }, } 

upgrade from v4 to v5 => https://webpack.js.org/migrate/5/#clean-up-configuration

like image 157
Maqsood Ahmed Avatar answered Sep 29 '22 08:09

Maqsood Ahmed


I think most the answers here would resolve your issue. However, if you don't need Polyfills for your node development, then I suggest using target: 'node' in your Webpack module configuration. This helped resolve the issue for me.

Here is some documentation on the answer: https://webpack.js.org/concepts/targets/

enter image description here

like image 43
Tr1gZer0 Avatar answered Sep 29 '22 06:09

Tr1gZer0