webpack使用CopyWebpackPlugin插件一直报错,麻烦大佬看看?

新手上路,请多包涵
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [],
  },
  plugins: [
    new CopyWebpackPlugin([
      // {output}/file.txt
      { from: path.resolve(__dirname, './src/assets/'), },
    ]),
  ],
};

一直给我提示这个错误,我想知道到底错在哪?按照文档上配的,百度、google都没这个错误的。大致意思是缺少{ from: path.resolve(__dirname, './src/assets/'), }配置,我现在实在找不到错误在哪,麻烦大佬指明一下

Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
 - options[0] misses the property 'patterns'. Should be:
   [non-empty string | object { from, to?, context?, globOptions?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)
ValidationError: Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
    at validate (D:\vscode-workspace\brower-manager\node_modules\copy-webpack-plugin\node_modules\schema-utils\dist\validate.js:88:11)
    at new CopyPlugin (D:\vscode-workspace\brower-manager\node_modules\copy-webpack-plugin\dist\index.js:24:30)
    at Object.<anonymous> (D:\vscode-workspace\brower-manager\webpack.common.js:14:5)
    at Module._compile (D:\vscode-workspace\brower-manager\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (D:\vscode-workspace\brower-manager\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
    at Object.<anonymous> (D:\vscode-workspace\brower-manager\webpack.dev.js:2:16)
    at Module._compile (D:\vscode-workspace\brower-manager\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (D:\vscode-workspace\brower-manager\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! brower-manager@1.0.0 dev: `webpack --config webpack.dev.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the brower-manager@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
阅读 25.4k
2 个回答

仔细看错误信息,缺少了 patterns 字段。实际上 copy-webpack-plugin 从 6.0 以后,把配置改成了

new CopyWebpackPlugin(
  patterns:
    [
      { from: path.resolve(__dirname, './src/assets/'), },
    ]
  ),
)  

参考:copy-webpack-plugin。建议以后先查官方文档,会节省很多时间。

是不是少了to的配置

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进