TAGS :Viewed: 2 - Published at: a few seconds ago

[ Mocha cant load module because it is a webpack external ]

I have an external set in my webpack.config.js that some of my ES6 modules import:

// webpack config:
externals: {
  'ExternalConfig': JSON.stringify(require('./config/config.dev.json'))
}
// ES6 modules:
import ExternalConfig from 'ExternalConfig'

When I run mocha tests on modules that import the external as a module then Mocha throws Error: Cannot find module 'ExternalConfig'

Is there any way I can pass the external to mocha to fix this issue?

Answer 1


Better you can require the json at the beginning of the webpack like,

const devConfig = require('./config/config.dev.json');

 // webpack config:
 externals: {
   'ExternalConfig': JSON.stringify(devConfig);
 }

 // ES6 modules:
 import ExternalConfig from 'ExternalConfig'

Try this

Answer 2


As mentioned by jhnns the webpack externals is not the place for loading config. I now load my config file based on the node process env global. This means mocha does not need to run webpack config and works as expected.