Node backpack exclude file from bundling

Viewed 140

I have the simplest backpack config like this:

module.exports = {
    webpack: (config, options, webpack) => {
      config.entry.main = [
        './app.js'
      ]
      return config;
    }
}

But inside my app I have

const myConfig = require('./config/someConfiguration.json');

This file ./config/someConfiguration.json should not be bundled so that it can be modified/configured. I tried to do it like that:

module.exports = {
    webpack: (config, options, webpack) => {
      config.entry.main = [
        './app.js'
      ];
      config.externals = [
        './config/someConfiguration.json'
      ];
      return config;
    }
}

But then I had 2 issues. One small one was that now I have to include the file like that '../config/someConfiguration.json' since it is required from the ./build/main.js bundled script. The other issue was that MySQL module started to throw exception out of nowhere:

    this.handshake = Packets.Handshake.fromPacket(helloPacket);
                                       ^
TypeError: Cannot read property 'fromPacket' of undefined...

Which I think means that I am not excluding the file from bundling as it should be. What's the proper way to solve this problem?

1 Answers

Externals are only for modules/librairies, do not use them for single files.
Use loaders instead like this :

config.module.loaders = [
    {
         test: /\.json$/,
         loader: 'json-loader'
    }
];

You have to install the json-loader package therefore.

Related