Webpack已经被初始化,但使用的配置对象与API模式不匹配。

4

使用 Ruby on Rails (6.1.4.4) 版本,在将 webpack 从 4.46.0 升级到 5.67.0,以及将 webpack-cli 版本从 3.3.12 升级到 4.9.2 后,经常出现以下错误:

root@b26d476dafbd:/myapp$ bin/webpack
[webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
 - configuration.node should be one of these:
   false | object { __dirname?, __filename?, global? }
   -> Include polyfills or mocks for various node stuff.
   Details:
    * configuration.node has an unknown property 'dgram'. These properties are valid:
      object { __dirname?, __filename?, global? }
      -> Options object for node compatibility features.
    * configuration.node has an unknown property 'fs'. These properties are valid:
      object { __dirname?, __filename?, global? }
      -> Options object for node compatibility features.
    * configuration.node has an unknown property 'net'. These properties are valid:
      object { __dirname?, __filename?, global? }
      -> Options object for node compatibility features.
    * configuration.node has an unknown property 'tls'. These properties are valid:
      object { __dirname?, __filename?, global? }
      -> Options object for node compatibility features.
    * configuration.node has an unknown property 'child_process'. These properties are valid:
      object { __dirname?, __filename?, global? }
      -> Options object for node compatibility features.

这是我的 package.json 文件的一个示例:

{
  "name": "vspm",
  "private": true,
  "dependencies": {
    "@claviska/jquery-minicolors": "^2.3.6",
    "@rails/activestorage": "^7.0.1",
    "@rails/ujs": "^7.0.1",
    "@rails/webpacker": "5.4.3",
    "add": "^2.0.6",
    "bootstrap": "5.1.3",
    "bootstrap4-tagsinput": "^4.1.3",
    "bufferutil": "^4.0.6",
    "cocoon": "github:nathanvda/cocoon#c24ba53",
    "datatables.net": "^1.11.4",
    "datatables.net-bs4": "^3.2.2",
    "google-libphonenumber": "^3.2.26",
    "intl-tel-input": "^17.0.15",
    "jquery": "^3.6.0",
    "jquery-mask-plugin": "^1.14.16",
    "justgage": "^1.5.1",
    "moment": "^2.29.1",
    "node-waves": "^0.7.6",
    "popper.js": "^1.16.1",
    "raphael": "^2.3.0",
    "textlint": "^12.1.0",
    "utf-8-validate": "^5.0.8",
    "webpack": "^5.67.0",
    "webpack-cli": "^4.9.2",
    "yarn": "^1.22.17"
  },
  "description": "This README would normally document whatever steps are necessary to get the application up and running.",
  "version": "1.0.0",
  "main": "pdf.js",
  "directories": {
    "lib": "lib",
    "test": "test"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/altjx/goku.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/altjx/goku/issues"
  },
  "homepage": "https://github.com/altjx/goku#readme",
  "devDependencies": {
    "@babel/plugin-transform-strict-mode": "^7.16.7",
    "webpack-dev-server": "^4.7.3"
  }
}

我不确定这个升级会导致什么问题。不幸的是,我甚至不知道错误信息实际指向了哪里。


通常这种错误来自于 config/webpacker.yml 文件。请更新该文件。 - razvans
1个回答

1
“看起来在Webpack 4中,你设置了节点polyfills,但在Webpack 5中自动删除了Node.js polyfills
在Webpack 5中设置polyfills的最简单方法是npm i node-polyfill-webpack-plugin。在webpack.config.js中使用它。”
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");

plugins: [
  new NodePolyfillPlugin(),
],

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接