简体   繁体   English

您如何解决“路径错误必须是字符串。 在webpack4 / copy-webpack-plugin中收到“未定义”

[英]How do you fix “ERROR in Path must be a string. Received undefined” in webpack4/copy-webpack-plugin

What I'm trying to achieve is to create a separate webpack assets file for copying static assets from the src folder to the web folder. 我要实现的目标是创建一个单独的webpack资产文件,以将静态资产从src文件夹复制到web文件夹。

node version: 8.15.0
yarn version: 1.13.0
webpack: 4.19.1
copy-webpack-plugin: 6.0.0

To start, I already have a webpack.common.js file which deals with all the js files, and I have created the assets file, which can be seen below. 首先,我已经有一个处理所有js文件的webpack.common.js文件,并且我已经创建了资产文件,如下所示。

When I run 当我跑步

webpack --config=webpack/webpack.assets.js --mode development --progress --color

or 要么

webpack --config=webpack/webpack.config.js --config=webpack/webpack.assets.js --mode development --progress --color --env development

I get this error ERROR in Path must be a string. Received undefined 我收到此错误ERROR in Path must be a string. Received undefined错误ERROR in Path must be a string. Received undefined ERROR in Path must be a string. Received undefined and I can't figure it out where it comes from. ERROR in Path must be a string. Received undefined ,我无法弄清楚它的来源。

By the way I just started dealing with webpack recently. 顺便说一下,我最近才刚开始处理webpack。

webpack.common.js

const webpack = require('webpack');
const path = require('path');

const PATHS = {
    src: path.join(process.cwd(), 'src', 'js'),
    dist: path.join(process.cwd(), 'web', 'js')
};

module.exports = {
    entry: {
        homepage: path.resolve(PATHS.src, 'pages/homepage.js'),
        otherfile: path.resolve(PATHS.src, 'pages/othefile.js'),
    }
    output: {
        path: PATHS.dist,
        filename: '[name].js',
        chunkFilename: '[name].js',
        publicPath: '/js/'
    },
    ...
}

webpack.assets.js

const webpack = require('webpack');
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');

const PATHS = {
    src: path.join(process.cwd(), 'src', 'svg'),
    dist: path.join(process.cwd(), 'web', 'svg')
};

module.exports = (env) => {
    const svgFormat = env === 'production' ? '[name].[hash].[ext]' : '[name].[ext]';

    return merge(commmonConfig, {
        entry: [
            path.resolve(PATHS.src, 'logo1.svg'),
            path.resolve(PATHS.src, 'logo2.svg')
        ],
        output: {
            path: PATHS.dist
        },
        module: {
            rules: [
                {
                    test: /\.(svg)$/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: svgFormat,
                            },
                        },
                    ],
                },
            ]
        },
        plugins: [
            new CopyPlugin([
                {
                    from: PATHS.src,
                    to: PATHS.dist,
                    force: true,
                    toType: 'dir'
                },
                {
                    copyUnmodified: true,
                    debug: 'debug'
                }
            ])
        ]
    });
};

What I would like is to be able to run the assets commands with no errors, as the actual files get copied correctly. 我想要的是能够正确运行资产命令,因为可以正确复制实际文件。

Any ideas are very much appreciated! 任何想法都非常感谢!

You have passed your options object as a second pattern. 您已将options对象作为第二种模式传递。

Move it outside of the patterns array and pass it as the second parameter instead: 将其移出patterns数组之外,并将其作为第二个参数传递:

plugins: [
  new CopyPlugin(
    [
      {
        from: PATHS.src,
        to: PATHS.dist,
        force: true,
        toType: 'dir'
      }
    ],
    {
      copyUnmodified: true,
      debug: 'debug'
    }
  )
]

You get the error as your options object is being treated as a pattern but does not have a from property. 由于选项对象被视为模式但没有from属性,因此会出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 webpack4复制webpack插件错误:意外字符'@' - webpack4 copy-webpack-plugin error: Unexpected Character '@' 如何在 webpack 5 copy-webpack-plugin 中使用展平设置 - How to use flatten setting in webpack 5 copy-webpack-plugin ““路径”参数必须是字符串。收到未定义的“如何解决此错误 - "The "path" argument must be string. Received undefined"how do I resolve this error Webpack 类型错误'TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 接收类型布尔值(真)' - Webpack type error 'TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type boolean (true)' 升级到 Webpack 5 后无法访问 copy-webpack-plugin 中的清单 - after upgrade to Webpack 5 not able to access the menifest in copy-webpack-plugin 使用Webpack和copy-webpack-plugin导入静态文件 - import static files using Webpack with copy-webpack-plugin copy-webpack-plugin不会复制文件 - copy-webpack-plugin doesn't copy files 自定义Gulp.js插件:TypeError“路径必须是一个字符串。 收到undefined“ - Custom Gulp.js plugin: TypeError “Path must be a string. Received undefined” Grunt“watch”警告:Path必须是一个字符串。 收到undefined - Grunt “watch” Warning: Path must be a string. Received undefined Jest TypeError:路径必须是字符串。 收到未定义 - Jest TypeError: Path must be a string. Received undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM