Webpack Configuration: Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema

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

Webpack Configuration: Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema

问题

以下是翻译好的部分:

我正在尝试首次设置Webpack配置。当我运行应用程序时,出现错误消息“配置对象无效。Webpack已使用不匹配API架构的配置对象进行初始化”。以下是我的webpack.config.js文件中的代码。

  1. const path = require("path");
  2. const webpack = require("webpack");
  3. const CopyWebpackPlugin = require("copy-webpack-plugin");
  4. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  5. const IS_DEVELOPMENT = process.env.NODE_ENV === "dev";
  6. const dirApp = path.join(__dirname, "app");
  7. const dirImages = path join(__dirname, "images");
  8. const dirVideo = path.join(__dirname, "videos");
  9. const dirStyles = path.join(__dirname, "styles");
  10. const dirShared = path.join(__dirname, "shared");
  11. const dirNode = "node_modules";
  12. module.exports = {
  13. entry: [path.join(dirApp, "index.js"), path.join(dirStyles, "index.scss")],
  14. resolve: {
  15. modules: [dirApp, dirShared, dirStyles, dirVideo, dirImages, dirNode],
  16. },
  17. plugins: [
  18. new webpack.DefinePlugin({
  19. IS_DEVELOPMENT,
  20. }),
  21. ]
  22. };

以下是我的webpack.config.development.js文件中的代码:

  1. const path = require("path");
  2. const { CleanWebpackPlugin } = require("clean-webpack-plugin");
  3. const { merge } = require("webpack-merge");
  4. const config = require("./webpack.config");
  5. module.exports = merge(config, {
  6. node: "production",
  7. output: {
  8. path: path.join(__dirname, "public"),
  9. },
  10. plugins: [new CleanWebpackPlugin()],
  11. });
英文:

I am trying to setup a webpack configuration for the first time. I am getting the error "Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema" when i run the app. Here is the code in my webpack.config.js file.

  1. const path = require("path");
  2. const webpack = require("webpack");
  3. const CopyWebpackPlugin = require("copy-webpack-plugin");
  4. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  5. const IS_DEVELOPMENT = process.env.NODE_ENV === "dev";
  6. const dirApp = path.join(__dirname, "app");
  7. const dirImages = path.join(__dirname, "images");
  8. const dirVideo = path.join(__dirname, "videos");
  9. const dirStyles = path.join(__dirname, "styles");
  10. const dirShared = path.join(__dirname, "shared");
  11. const dirNode = "node_modules";
  12. module.exports = {
  13. entry: [path.join(dirApp, "index.js"), path.join(dirStyles, "index.scss")],
  14. resolve: {
  15. modules: [dirApp, dirShared, dirStyles, dirVideo, dirImages, dirNode],
  16. },
  17. plugins: [
  18. new webpack.DefinePlugin({
  19. IS_DEVELOPMENT,
  20. }),
  21. ]
  22. };
  23. ;

Here is the code in my webpack.config.development.js file

  1. const path = require("path");
  2. const { CleanWebpackPlugin } = require("clean-webpack-plugin");
  3. const { merge } = require("webpack-merge");
  4. const config = require("./webpack.config");
  5. module.exports = merge(config, {
  6. node: "production",
  7. output: {
  8. path: path.join(__dirname, "public"),
  9. },
  10. plugins: [new CleanWebpackPlugin()],
  11. });

答案1

得分: 1

Change node: "production" to mode: "production" in webpack.config.development.js file.

I have used the updated configuration.

  1. const path = require("path");
  2. const { CleanWebpackPlugin } = require("clean-webpack-plugin");
  3. const { merge } = require("webpack-merge");
  4. const config = require("./webpack.config");
  5. module.exports = merge(config, {
  6. mode: "production",
  7. output: {
  8. path: path.join(__dirname, "public"),
  9. },
  10. plugins: [new CleanWebpackPlugin()],
  11. });
  1. const path = require("path");
  2. const webpack = require("webpack");
  3. const CopyWebpackPlugin = require("copy-webpack-plugin");
  4. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  5. const IS_DEVELOPMENT = process.env.NODE_ENV === "dev";
  6. const dirApp = path.join(__dirname, "app");
  7. const dirImages = path.join(__dirname, "images");
  8. const dirVideo = path.join(__dirname, "videos");
  9. const dirStyles = path.join(__dirname, "styles");
  10. const dirShared = path.join(__dirname, "shared");
  11. const dirNode = "node_modules";
  12. module.exports = {
  13. entry: [path.join(dirApp, "index.js"), path.join(dirStyles, "index.scss")],
  14. resolve: {
  15. modules: [dirApp, dirShared, dirStyles, dirVideo, dirImages, dirNode],
  16. },
  17. plugins: [
  18. new webpack.DefinePlugin({
  19. IS_DEVELOPMENT,
  20. }),
  21. ]
  22. };

By using the above configuration, it builds successfully. I don't have any data in index.js and index.scss files.

Webpack stats: asset main.js 0 bytes [emitted] [minimized] (name: main)
./app/index.js 1 bytes [built] [code generated]
./styles/index.scss 1 bytes [built] [code generated]
webpack 5.78.0 compiled successfully in 170 ms
Your app is compiled in production mode in /dist. It's ready to roll!

英文:

Change node: "production" to mode: "production" in webpack.config.development.js file.
Generally when you have wrong configuration then it will show this error. You can check configuration of Webpack at respective version Webpack 5

Also you used merge but it accept the config object to be merge while in your case webpack.config.js do not export any configuration.

I have used the updated configuration.

  1. const path = require("path");
  2. const { CleanWebpackPlugin } = require("clean-webpack-plugin");
  3. const { merge } = require("webpack-merge");
  4. const config = require("./webpack.config");
  5. module.exports = merge(config, {
  6. mode: "production",
  7. output: {
  8. path: path.join(__dirname, "public"),
  9. },
  10. plugins: [new CleanWebpackPlugin()],
  11. });
  1. const path = require("path");
  2. const webpack = require("webpack");
  3. const CopyWebpackPlugin = require("copy-webpack-plugin");
  4. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  5. const IS_DEVELOPMENT = process.env.NODE_ENV === "dev";
  6. const dirApp = path.join(__dirname, "app");
  7. const dirImages = path.join(__dirname, "images");
  8. const dirVideo = path.join(__dirname, "videos");
  9. const dirStyles = path.join(__dirname, "styles");
  10. const dirShared = path.join(__dirname, "shared");
  11. const dirNode = "node_modules";
  12. module.exports = {
  13. entry: [path.join(dirApp, "index.js"), path.join(dirStyles, "index.scss")],
  14. resolve: {
  15. modules: [dirApp, dirShared, dirStyles, dirVideo, dirImages, dirNode],
  16. },
  17. plugins: [
  18. new webpack.DefinePlugin({
  19. IS_DEVELOPMENT,
  20. }),
  21. ]
  22. };

By using above configuration it build successfully. I don't have any data in index.js and index.scss file.

  1. Webpack stats: asset main.js 0 bytes [emitted] [minimized] (name: main)
  2. ./app/index.js 1 bytes [built] [code generated]
  3. ./styles/index.scss 1 bytes [built] [code generated]
  4. webpack 5.78.0 compiled successfully in 170 ms
  5. Your app is compiled in production mode in /dist. It's ready to roll!

huangapple
  • 本文由 发表于 2023年4月11日 12:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982426.html
匿名

发表评论

匿名网友

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

确定