Webpack开发服务器初始化问题

huangapple go评论118阅读模式
英文:

Webpack dev server initialization problem

问题

当我在命令提示符中输入npm run dev时,它给我返回以下错误:

无效的配置对象。Webpack已经使用不符合API模式的配置对象进行初始化。

完整的跟踪信息:

  • configuration.module.rules1.use应该是以下之一:
    非空字符串 | 函数 | 对象{ ident?, loader?, options?, query? } | 函数 | [非空字符串 | 函数 | 对象{ ident?, loader?, options?, query? }]
    -> 当规则匹配时应用于模块的修饰符
    详情:

    • configuration.module.rules1.use[0]应该是一个字符串。
    • configuration.module.rules1.use[0]应该是一个函数实例。
    • configuration.module.rules1.use[0]应该是一个对象。
    • configuration.module.rules1.use[0]应该是以下之一:
      非空字符串 | 函数 | 对象{ ident?, loader?, options?, query? }
    • configuration.module.rules1.use[0]应该是以下之一:
      非空字符串 | 函数 | 对象{ ident?, loader?, options?, query? }
      -> 一个use项

我的webpack配置文件如下:

const path = require("path");
const extractCssPlugin = require("mini-css-extract-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

const JS_DIR = path.resolve(__dirname, "js");
const BUILD = path.resolve(__dirname, "build");

// JS的入口点
entry = {
main: JS_DIR + "main.js",
};

// 输出的入口点
output = {
path: BUILD,
filename: 'js/[name].js'
};

// webpack的规则

const rules = [

  1. // JavaScript的规则
  2. {
  3. test: /\.js$/,
  4. include: [ JS_DIR ],
  5. exclude: /node_modules/,
  6. use: 'babel-loader'
  7. },
  8. // scss的规则
  9. {
  10. test: /\.scss$/,
  11. exclude: /node_modules/,
  12. use: [
  13. extractCssPlugin.load,
  14. "css-loader"
  15. ]
  16. },
  17. // 图片的规则
  18. {
  19. test: /\.(png|jpg|jpeg)$/,
  20. use: [
  21. {
  22. loader: "file-loader",
  23. options: {
  24. name: "[path][name].[ext]",
  25. publicPath: ( process.env.NODE_ENV === "production" ) ? "../" : "../../"
  26. }
  27. }
  28. ]
  29. }

]

// 定义插件

const plugins = argv => {
return [
new CleanWebpackPlugin({
cleanStaleWebpackAssets: argv.mode === "production"
}),
new extractCssPlugin({
filename: "css/[name].css"
})
]
}

module.exports = ( env, argv ) => {
return {
entry: entry,
output: output,
module: {
rules: rules
},
plugins: plugins( argv )
}
}

奇怪的是,开发服务器在我的笔记本电脑上运行得很好,但在我的台式电脑上却不行,尽管我已经安装了所有必需的软件包。我的代码有什么问题?

英文:

When I write npm run dev in cmd, it is giving me following error:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schem
a.

Full Trace:

  1. - configuration.module.rules[1].use should be one of these:
  2. non-empty string | function | object { ident?, loader?, options?, query? } | function | [non-empty string | function |
  3. object { ident?, loader?, options?, query? }]
  4. -> Modifiers applied to the module when rule is matched
  5. Details:
  6. * configuration.module.rules[1].use[0] should be a string.
  7. * configuration.module.rules[1].use[0] should be an instance of function
  8. * configuration.module.rules[1].use[0] should be an object.
  9. * configuration.module.rules[1].use[0] should be one of these:
  10. non-empty string | function | object { ident?, loader?, options?, query? }
  11. * configuration.module.rules[1].use[0] should be one of these:
  12. non-empty string | function | object { ident?, loader?, options?, query? }
  13. -> An use item

My webpack configuration file:

  1. const path = require("path");
  2. const extractCssPlugin = require("mini-css-extract-plugin");
  3. const { CleanWebpackPlugin } = require("clean-webpack-plugin");
  4. const JS_DIR = path.resolve(__dirname, "js");
  5. const BUILD = path.resolve(__dirname, "build");
  6. // Entry point of our JS
  7. entry = {
  8. main: JS_DIR + "main.js",
  9. };
  10. // Entry point of the output
  11. output = {
  12. path: BUILD,
  13. filename: 'js/[name].js'
  14. };
  15. // Rules for the webpack
  16. const rules = [
  17. // Rule for the JavaScript
  18. {
  19. test: /\.js$/,
  20. include: [ JS_DIR ],
  21. exclude: /node_modules/,
  22. use: 'babel-loader'
  23. },
  24. // Rule for the scss
  25. {
  26. test: /\.scss$/,
  27. exclude: /node_modules/,
  28. use: [
  29. extractCssPlugin.load,
  30. "css-loader"
  31. ]
  32. },
  33. // Rule for the images
  34. {
  35. test: /\.(png|jpg|jpeg)$/,
  36. use: [
  37. {
  38. loader: "file-loader",
  39. options: {
  40. name: "[path][name].[ext]",
  41. publicPath: ( process.env.NODE_ENV === "production" ) ? "../" : "../../"
  42. }
  43. }
  44. ]
  45. }
  46. ]
  47. // Defining the plugins
  48. const plugins = argv => {
  49. return [
  50. new CleanWebpackPlugin({
  51. cleanStaleWebpackAssets: argv.mode === "production"
  52. }),
  53. new extractCssPlugin({
  54. filename: "css/[name].css"
  55. })
  56. ]
  57. }
  58. module.exports = ( env, argv ) => {
  59. return {
  60. entry: entry,
  61. output: output,
  62. module: {
  63. rules: rules
  64. },
  65. plugins: plugins( argv )
  66. }
  67. }

Strangely dev server is running perfectly in my Laptop but not in my Computer even though I installed all required packages. What is wrong in my code?

答案1

得分: 1

看到你的webpack配置,我可以看出问题出在这部分:

// Rule for the scss
{
test: /.scss$/,
exclude: /node_modules/,
use: [
extractCssPlugin.load,
"css-loader"
]
},

问题在于你在"use"数组中使用了"extractCssPlugin.load"作为值,这导致了错误。根据mini-css-extract-plugin的官方文档,你应该使用"extractCssPlugin"对象中的"MiniCssExtractPlugin.loader"引用。

以下是你应该修改的SCSS规则:

// Rule for the scss
{
test: /.scss$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
},

确保你的webpack配置文件顶部也有必要的导入:

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

希望这样可以解决SCSS规则的问题,并帮助你在电脑上运行webpack开发服务器。

英文:

Looking at your webpack configuration, I can see that the issue is in this part:

  1. // Rule for the scss
  2. {
  3. test: /\.scss$/,
  4. exclude: /node_modules/,
  5. use: [
  6. extractCssPlugin.load,
  7. "css-loader"
  8. ]
  9. },

The problem here is that you are using extractCssPlugin.load as a value in the use array, which is causing the error. Instead, based on the offical docs for mini-css-extract-plugin you should use the MiniCssExtractPlugin.loader reference from the extractCssPlugin object.

Here's how you should modify the SCSS rule:

  1. // Rule for the scss
  2. {
  3. test: /\.scss$/,
  4. exclude: /node_modules/,
  5. use: [
  6. MiniCssExtractPlugin.loader,
  7. "css-loader"
  8. ]
  9. },

Make sure you also have the necessary imports at the top of your webpack configuration file:

  1. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  2. const { CleanWebpackPlugin } = require("clean-webpack-plugin");

I hope this should resolve the issue with the SCSS rule and help you get the webpack dev server running on your computer as well.

huangapple
  • 本文由 发表于 2023年8月9日 03:52:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定