Error when trying to copy an empty folder with webpack
Asked Answered
R

2

22

Using Webpack and copy-webpack-plugin I need to copy an entire folder during build and I have:

plugins: [
  new CopyPlugin({
    patterns: [
      { from: './src/assets', to: './assets' }
    ]
  })
]

However this fails when the assets folder is empty:

ERROR: unable to locate ...

If I add one file to assets folder i will work.

How to solve this?

Rallentando answered 10/9, 2020 at 15:34 Comment(0)
A
36

Just add the property noErrorOnMissing to the patterns first object and set it to true. From what I understood, this will avoid generating an error if no file is present in the "from" destination folder, but at the same time it wont add the empty folder to your output folder (at least in my own test). This is the extract from one of my projects:

new CopyWebpackPlugin({
            patterns: [
                {
                    from: __dirname + '/src/assets/images',
                    to: 'assets/images',
                    noErrorOnMissing: true
                }
            ]
        })

Source: https://webpack.js.org/plugins/copy-webpack-plugin/#noerroronmissing

Andel answered 21/9, 2020 at 22:56 Comment(1)
+1. Helped me in 2021. Thanks! To add a little context, it could also be if the filter specified for CopyWebpackPlugin via globOptions/ignore did not yield any file to copy. So not necessarily an empty folder.Chough
S
0

If you're using git, you can place an empty .gitkeep file in that folder.

Use it while you don't have files in this folder: it's just placeholder used in most projects for empty folders, since Git tracks only files.

Spiegleman answered 6/12, 2022 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.