报错BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modu

376 阅读1分钟

报错 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 }

image.png

这个错误是由于Webpack 5之前的版本默认会为Node.js核心模块包含polyfills,但在Webpack 5中不再默认提供这些polyfills,需要手动配置。

你可以通过以下步骤解决这个问题:

  1. 添加相应的polyfill来解决这个问题。你可以使用resolve.fallback配置项来添加所需的polyfill。 例如,你可以在webpack.config.js文件中添加以下代码:
javascriptCopy Code
module.exports = {
  // 其他配置项...
  resolve: {
    fallback: {
      "fs": false,
      "path": false
    }
  }
};

这样配置后,Webpack将不会报错关于"fs"和"path"模块缺失的错误。

2.若是vue项目 修改配置文件即可

方式一:

// vue.config.js  
module.exports = {  
    configureWebpack: {  
        resolve: {  
            fallback: { path: false },  
        },  
    } 
}

方式二:

安装依赖

npm i path-browserify

修改配置文件

// vue.config.js  
module.exports = {  
    configureWebpack: {  
        resolve: {  
            fallback: {  
                path: require.resolve('path-browserify'),  
            },  
        },  
    } 
}