首页 > 解决方案 > webpack 文件 webpack.config.js [加载失败]

问题描述

我在使用 webpack cli 时遇到了这个问题

我正在尝试运行这个文件 webpack:

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/app.js',
    output: {
        path: path.resolve(__dirname , '..dist'),
        filename: 'bundle.js'
    },
    plugins = [
        new HtmlWebPackPlugin({
            template: './src/index.hbs'
        })
    ]
}

sistem 的文件是这样的:

这是我的 package.son:

{
  "name": "webpack-workflow",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "autoprefixer": "^10.2.5",
    "css-loader": "^5.2.0",
    "file-loader": "^6.2.0",
    "handlebars": "^4.7.7",
    "handlebars-loader": "^1.7.1",
    "html-webpack-plugin": "^5.3.1",
    "image-webpack-loader": "^7.0.1",
    "mini-css-extract-plugin": "^1.4.1",
    "node-sass": "^5.0.0",
    "postcss-loader": "^5.2.0",
    "rimraf": "^3.0.2",
    "sass-loader": "^11.0.1",
    "webpack": "^5.31.0",
    "webpack-cli": "^4.6.0",
    "webpack-dev-server": "^3.11.2"
  }
}

所以从终端我输入:npx webpack --config webpack.config.js

这个错误正在显示

[webpack-cli] Failed to load 'C:\Users\Gerso\Documents\WebPack\webpack-workflow\webpack.config.js' config
[webpack-cli] Error: Cannot find module 'C:\Users\Gerso\Documents\WebPack\webpack-workflow\webpack.config.js'
Require stack:
- C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack-cli\lib\webpack-cli.js
- C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack-cli\lib\bootstrap.js
- C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack-cli\bin\cli.js
- C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack\bin\webpack.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\v8-compile-cache\v8-compile-cache.js:159:20)       
    at loadConfig (C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack-cli\lib\webpack-cli.js:1322:31)
    at C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack-cli\lib\webpack-cli.js:1409:74
    at Array.map (<anonymous>)
    at WebpackCLI.resolveConfig (C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack-cli\lib\webpack-cli.js:140    at WebpackCLI.createCompiler (C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack-cli\lib\webpack-cli.js:1839:33)
    at WebpackCLI.buildCommand (C:\Users\Gerso\Documents\WebPack\webpack-workflow\node_modules\webpack-cli\lib\webpack-cli.js:1954:31) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\Users\\Gerso\\Documents\\WebPack\\webpack-workflow\\node_modules\\webpack-cli\\lib\\webpack-cli.js',
    'C:\\Users\\Gerso\\Documents\\WebPack\\webpack-workflow\\node_modules\\webpack-cli\\lib\\bootstrap.js',
    'C:\\Users\\Gerso\\Documents\\WebPack\\webpack-workflow\\node_modules\\webpack-cli\\bin\\cli.js',
    'C:\\Users\\Gerso\\Documents\\WebPack\\webpack-workflow\\node_modules\\webpack\\bin\\webpack.js'
  ]
}
PS C:\Users\Gerso\Documents\WebPack\webpack-workflow> npx webpack --config webpack.config.js
[webpack-cli] Failed to load 'C:\Users\Gerso\Documents\WebPack\webpack-workflow\webpack.config.js' config
[webpack-cli] C:\Users\Gerso\Documents\WebPack\webpack-workflow\webpack.config.js:10
    plugins = [
    ^^^^^^^^^^^

我做错了什么?

标签: webpackwebpack-dev-serverwebpack-4

解决方案


在你的webpack.config.js

plugins = [

应该

plugins: [

要验证您的配置,您可以运行:

webpack configtest webpack.config.js

推荐阅读