24

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!

Nicolas Nicolas
  • 293
  • 1
  • 3
  • 10

14 Answers14

14

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
hjpithadia
  • 176
  • 1
  • 7
12

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.

Camilo Gomez
  • 165
  • 1
  • 6
  • Make sure you stop your server and start it again. Otherwise, thank you for the answer. – Kasasira Apr 28 '22 at 08:22
  • Near a year later, this fixed my issue. +1 Did you ever find a fix for that warnings? Thanks. – itchibahn Nov 15 '22 at 20:56
  • Yes, I had to update, react, react-scripts, all my packages and change some deprecated packages for others. But this solution is not for all projects. – Camilo Gomez Nov 16 '22 at 01:49
6

I have the same issue in React (feb-2022) using Real (realm-web) from Mongo.

I solve this in two steps:

  1. npm i stream-browserify crypto-browserify
  2. create fallback object into webpack.config.js at node_modules/react-scripts/config/webpack.config.js
module.exports = function (webpackEnv) {
  ...
  return {
   ...
    resolve: {
      ...
      fallback: { // not present by default
        "crypto": false,
        "stream": false

      }
    }
  }
Zyncho
  • 407
  • 6
  • 12
4

I have the same issue with crypto. Some say that adding a proper path in TS config (and installing a polyfill) should resolve the issue.

tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "crypto": [
        "./node_modules/crypto-browserify"
      ],
}

Details https://github.com/angular/angular-cli/issues/20819

I'm still fighting with crypto, but above link might help in your struggles.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Sielu
  • 1,070
  • 14
  • 19
2

I had the same issue, Strangely an import {script} from 'laravel-mix' in my app.js. I removed it and everything went back to normal.

1

I could not get the other answers to work with React on a new project with rss-parser. The error messages aren't clear. I fixed it by adding this line to the webpack configuration:

  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    fallback: { "http": false, "browser": false, "https": false, 
      "stream": false, "url": false, "buffer": false, "timers": false
    }
  },
0

Watch out for this import in your app.js. Removing it fixed my issue.

Watch out for this import in your app.js

kasre
  • 59
  • 3
0

use the version of webpack 4.46.0 to remove the error

  • This is often the super simple option if you're running react-scripts v5 (which is where this "problem" emerged). I'm running v4.0.0, and that's working). [Link to the SO answer I found](https://stackoverflow.com/questions/70591567/module-not-found-error-cant-resolve-fs-in-react) If you *do* need to stay on v5 and/or configure specific modules, I found [this SO answer](https://stackoverflow.com/questions/57161839/module-not-found-error-cant-resolve-fs-in) useful. – Mike Apr 07 '22 at 18:46
  • The problem coming in v5.0, providing a solution to downgrade to v4.x is not ideal. – Varun Apr 22 '22 at 18:24
0

I had to delete this line in a Vue component to solve the problem:

import { ContextReplacementPlugin } from "webpack";

0

If you use create-react-app(cra)

  1. You have to overwrite the default webpack file to do that you need to install a package like "react-app-rewired" that will help overwriting and merge your configuration with the ones were added by react scripts First
 npm install --D react-app-rewired
  1. create a new file in the root directory called "config-overrides.js"
  2. Install(using npm) and Add the required dependencies to the created config file
npm i -D path-browserify
install other packages as the error message shows
const webpack = require('webpack');

module.exports = function override(config) {
  const fallback = config.resolve.fallback || {};
  Object.assign(fallback, {
    path: require.resolve('path-browserify'),
    add others as shown in the error if there are more
  });
  config.resolve.fallback = fallback;
  config.plugins = (config.plugins || []).concat([
    new webpack.ProvidePlugin({
      process: 'process/browser',
      Buffer: ['buffer', 'Buffer'],
    }),
  ]);
  return config;
};

  1. Update your package.json scripts
 "scripts": {
    "build": "react-app-rewired build",
     ....
    "start": "react-app-rewired start",
    "test": "react-app-rewired test"
  },

Also please note that a new version of react-scripts(newer than 5.0.1) might solve the issue.

Mohanad Walo
  • 384
  • 3
  • 6
0

had this issue in a react-app.
installation of stream-browserify crypto-browserify didn't work for me

went through with hjpithadia's answer in Reactjs with a slight change in webpack.config.js

  1. install the package npm install node-polyfill-webpack-plugin
  2. find the webpack configuration file here
node_modules/react-scripts/config/webpack.config.js
  1. import the package const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
  2. search for module.exports
  3. in plugin array add this new NodePolyfillPlugin(),
module.exports = function (webpackEnv) {
  ...
  return {
   ...
    plugins: [
      new NodePolyfillPlugin(),
      ...
    ]
  }
}
Naima Ghulam.M
  • 361
  • 3
  • 9
0

For React apps, if you don't need the module make sure webpack ignores it.

  1. install craco https://craco.js.org/docs/getting-started/

  2. create a craco.config.js in the root of your project, mine looks like this:

module.exports = {
    webpack: {
        configure: (webpackConfig) => {
            webpackConfig.resolve.fallback = webpackConfig.resolve.fallback || {}
            webpackConfig.resolve.fallback.zlib = false
            webpackConfig.resolve.fallback.http = false
            webpackConfig.resolve.fallback.https = false
            webpackConfig.resolve.fallback.stream = false
            webpackConfig.resolve.fallback.util = false
            webpackConfig.resolve.fallback.crypto = false
            return webpackConfig;
        },
    }
};
Mr Rogers
  • 6,091
  • 2
  • 32
  • 34
FrankyHollywood
  • 1,497
  • 19
  • 18
0

I resolved similar issue:

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: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
  1. Installing the npm i stream-browserify --save-dev
  2. Making the custom webpack.config.js file containing:
module.exports = {
  resolve: {
    fallback: { stream: require.resolve("stream-browserify") },
  },
};
  1. Installing the npm install @angular-builders/custom-webpack --save-dev

  2. Assigning (angular.json: projects -> <proj_name> -> architect -> build -> options)

"customWebpackConfig": {
  "path": "./webpack.config.js"
}
  1. Changing the: build / serve / extract-i18n / test – as of your needs – to use custom Webpack config:
"builder": "@angular-builders/custom-webpack:browser",
KGROZA
  • 63
  • 7
-3

I finally found the solution. With laravel, you have to change the webpack.mix.js file and not webpack.config.js.

Nicolas Nicolas
  • 293
  • 1
  • 3
  • 10