Next.js: Files copied with CopyWebPackPlugin disappear

Created on 12 Dec 2017  路  7Comments  路  Source: vercel/next.js


Files copied to '/static' using CopyWebpackPlugin are successfully created, but then are deleted right after the console messages 'Building page ...'.

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior



Files copied by CopyWebpackPlugin to '/static' don't subsequently get removed by the build process.

Current Behavior



Files copied by CopyWebpackPlugin to '/static' get copied, then deleted by the build process.

Steps to Reproduce (for bugs)



Run npm run dev with the following next.config.js (note that I'm copying a file from the workbox-sw npm package, but it can be any file):

const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
  webpack: (config, { dev }) => {
    config.plugins.push(
      new CopyWebpackPlugin([
      {
        from: path.join(__dirname, 'node_modules/workbox-sw/build/importScripts/workbox-sw.dev.v2.1.2.js'),
        to: path.join(__dirname, 'static/scripts/workbox.js')
      }
    ]))

    return config
  }
}

Context



Can't copy files at build time.

Your Environment


| Tech | Version |
|---------|---------|
| next | 4.1.4 |
| node | 9.2.0 |
| OS | OS X 10.11.6 |
| browser | Chrome 62 |
| etc | |

Most helpful comment

Hey @Organizzzm,

I'm using Next 7.0.2 and it works for me with this code:

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

module.exports = {
  webpack: (config, { isServer }) => {
    config.plugins.push(
      new CopyWebpackPlugin([
        {
          from: path.join(__dirname, 'node_modules/widget-editor/dist/images'),
          to: path.join(__dirname, 'static/images/widget-editor/')
        }
      ])
    );
    return config;
  }
};

Make sure you use path.join.

All 7 comments

Wasn't able to reproduce in my project - static files were copied and were present after build.
But I linked static files in _document.js as per comment in this issue.

---update
Indeed, when using npm run dev it deletes files, while next build && next start doesn't.

I have the same problem. it deletes files when using npm run dev.
while next build && next start doesn't <-- but no hot reloading..

webpack is ran twice, once for the server, once for the client, the solution might be as simple as:

const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
  webpack: (config, { dev, isServer }) => {
if(isServer) {
return config
}
    config.plugins.push(
      new CopyWebpackPlugin([
      {
        from: path.join(__dirname, 'node_modules/workbox-sw/build/importScripts/workbox-sw.dev.v2.1.2.js'),
        to: path.join(__dirname, 'static/scripts/workbox.js')
      }
    ]))

    return config
  }
}

If you want to implement workbox try https://github.com/hanford/next-offline

There's not much that we can change in Next.js to fix this issue, so I'm going to close it.

@timneutkens I also use the plugin CopyWebpackPlugin and the files are still deleted when I run npm run dev, even if I add the condition.

const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.plugins.push(
        new CopyWebpackPlugin([
          {
            context: 'node_modules/widget-editor/dist/images/',
            from: '**/*',
            to: 'static/images/widget-editor/'
          }
        ])
      );
    }

    return config;
  }
};

@clementprdhomme it's fixed in Next 5.1.

It still reproduce

Hey @Organizzzm,

I'm using Next 7.0.2 and it works for me with this code:

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

module.exports = {
  webpack: (config, { isServer }) => {
    config.plugins.push(
      new CopyWebpackPlugin([
        {
          from: path.join(__dirname, 'node_modules/widget-editor/dist/images'),
          to: path.join(__dirname, 'static/images/widget-editor/')
        }
      ])
    );
    return config;
  }
};

Make sure you use path.join.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

knipferrc picture knipferrc  路  3Comments

havefive picture havefive  路  3Comments

kenji4569 picture kenji4569  路  3Comments

wagerfield picture wagerfield  路  3Comments

YarivGilad picture YarivGilad  路  3Comments