npm run build:ValidationError:无效的选项对象。复制插件

Posted

技术标签:

【中文标题】npm run build:ValidationError:无效的选项对象。复制插件【英文标题】:npm run build: ValidationError: Invalid options object. Copy Plugin 【发布时间】:2020-10-02 23:56:21 【问题描述】:

我正在尝试打包一个电子应用程序,但我收到了这个错误:

ValidationError: 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)
    at validate (C:\Users\julis\Documents\prescription-data-entry-system\pdes\node_modules\copy-webpack-plugin\node_modules\schema-utils\dist\validate.js:96:11)
    at new CopyPlugin (C:\Users\julis\Documents\prescription-data-entry-system\pdes\node_modules\copy-webpack-plugin\dist\index.js:24:30)
    at Object.<anonymous> (C:\Users\julis\Documents\prescription-data-entry-system\pdes\.electron-vue\webpack.renderer.config.js:181:5)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (C:\Users\julis\Documents\prescription-data-entry-system\pdes\.electron-vue\build.js:14:24)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 
  name: 'ValidationError',
  errors: [
    
      keyword: 'required',
      dataPath: '[0]',
      schemaPath: '#/required',
      params: [Object],
      message: "should have required property 'patterns'",
      schema: [Object],
      parentSchema: [Object],
      data: [Object],
      children: [Array]
    
  ],
  schema: 
    definitions:  ObjectPattern: [Object], StringPattern: [Object] ,
    type: 'object',
    additionalProperties: false,
    properties:  patterns: [Object], options: [Object] ,
    required: [ 'patterns' ]
  ,
  headerName: 'Copy Plugin',
  baseDataPath: 'options',
  postFormatter: null,
  message: 'Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.\n' +
    " - options[0] misses the property 'patterns'. Should be:\n" +
    '   [non-empty string | object  from, to?, context?, globOptions?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? , ...] (should not have fewer than 1 item)'

.electron-vue\webpack.renderer.config.js,我有:

new CopyWebpackPlugin([
  
    from: path.join(__dirname, '../static'),
    to: path.join(__dirname, '../dist/electron/static'),
    ignore: ['.*']
  
]),

将其更改为以下,并不能解决错误:

    new CopyWebpackPlugin(

      patterns: [
         from: path.join(__dirname, '../static'), to: path.join(__dirname, '../dist/electron/static') ,
      ],

    ),

我不确定新模式应该是什么。我查看了this question 并尝试遵循该模式,但没有奏效。我怎样才能解决这个问题?

【问题讨论】:

【参考方案1】:

我必须在 .electron-vue\webpack.web.config.js 中更改相同的内容

/**
 * Adjust webConfig for production settings
 */
if (process.env.NODE_ENV === 'production') 
  webConfig.devtool = ''

  webConfig.plugins.push(
    new MinifyPlugin(),
    new CopyWebpackPlugin(
      //this is the new change
      patterns: [
         from: path.join(__dirname, '../static'), to: path.join(__dirname, '../dist/electron/static') ,
      ],

    ),
    new webpack.DefinePlugin(
      'process.env.NODE_ENV': '"production"'
    ),
    new webpack.LoaderOptionsPlugin(
      minimize: true
    )
  )

【讨论】:

以上是关于npm run build:ValidationError:无效的选项对象。复制插件的主要内容,如果未能解决你的问题,请参考以下文章

npm run build 时的 warning

npm run build 给出 npm ERR!代码生命周期

在打包(npm run build)的时候不执行了,但是也不报错,输入npm run dev可以启动

运行 npm run build 时出现构建错误

运行 npm run build 时出现构建错误

npm run build打包遇到的问题