Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default

I'm trying to use Socket.io in my Laravel/Vue.js app. But I'm getting this error when running npm run dev.

Module not found: Error: Can't resolve 'path' in '/home/nicolas/LaravelProjects/Chess/node_modules/socket.io/dist'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
    - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "path": false }

webpack compiled with 9 errors

I tried changing the webpack.config.js file by adding resolve.fallback: { "path": false }, but it doesn't seem to help. It is possible that I'm doing it wrong since I have 4 webpack.config.js files in my project (I don't know why).

Maybe can I downgrade webpack? Thanks!

like image 305
Nicolas Nicolas Avatar asked Sep 11 '25 21:09

Nicolas Nicolas


2 Answers

This fix worked for me (Vue 3):

  1. In vue.config.js, add:
const { defineConfig } = require('@vue/cli-service')
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack: {
    plugins: [
      new NodePolyfillPlugin()
    ]
  }

})
  1. Followed by npm install node-polyfill-webpack-plugin
like image 92
hjpithadia Avatar answered Sep 13 '25 09:09

hjpithadia


I had this problem in ReactJS with create-react-app(facebook) but other packages (crypto-browserify)

Solution:

  1. First install the necessary packages in this problem "path-browserify" but in my case "crypto-browserify"

  2. Modify webpack.config.js in reactjs with create-react-app this file is inside:

node_modules/react-scripts/config/webpack.config.js

  • Search module.exports and inside this function there is a return:
module.exports = function (webpackEnv) {
  ...
  return {
   ...
    resolve: {
      ...
      fallback: {
        // Here paste
        path: require.resolve("path-browserify"),
        // But in mi case I paste
        crypto: require.resolve("crypto-browserify"),

      }
    }
  }
}

Note: This solution works, but when the webpack project starts it shows warnings

Pd: I am not native speaker English, but I hope understand me.

like image 31
Camilo Gomez Avatar answered Sep 13 '25 09:09

Camilo Gomez