报错内容如下:

E:\yygh\webpackdemo>webpack --mode=development
[webpack-cli] Failed to load 'E:\yygh\webpackdemo\webpack.config.js' config
[webpack-cli] ReferenceError: path is not defined
    at Object.<anonymous> (E:\yygh\webpackdemo\webpack.config.js:5:15)
    at Module._compile (C:\Users\DELL\AppData\Roaming\npm\node_modules\webpack-cli\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (C:\Users\DELL\AppData\Roaming\npm\node_modules\webpack-cli\node_modules\v8-compile-cache\v8-compile-cache.js:159:20)
    at loadConfig (C:\Users\DELL\AppData\Roaming\npm\node_modules\webpack-cli\lib\webpack-cli.js:1322:31)
    at WebpackCLI.resolveConfig (C:\Users\DELL\AppData\Roaming\npm\node_modules\webpack-cli\lib\webpack-cli.js:1454:44)

这是我新手入门前端的一个坑,以前没注意过,自从改装了webpack4后,出现了上面的因为版本更新连带的错误。
解决办法:
将webpack.config.js中的信息修改一下

constpath=require("path") //Node.js内置模块
module.exports= {
    entry: './src/main.js', //配置入口文件
    output: {
        path: path.resolve(__dirname, './dist'), //输出路径,__dirname:当前文件所在路径
        filename: 'bundle.js'//输出文件
    }
}

改成下面的

const path = require('path')
module.exports = {
  entry: path.join(__dirname, 'src/main.js'),
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist')
  }
}
Logo

前往低代码交流专区

更多推荐