javascripttypescriptwebpacknestjsbull.js

Bull.js is not getting the exported function in NestJS with webpack


My NestJS application compiles my Typescript worker

async function bootstrap(job: Job, done: DoneCallback) {
  //...
}
export default bootstrap;

into a js file with following line:

exports["default"] = bootstrap;

But Bull.js is throwing the following error:

/usr/src/app/node_modules/bull/lib/job.js:516
          reject(new Error(failedReason));
                 ^
Error: origProcessor.apply is not a function

It only works if I manually add into the compiled js file:

module.exports = bootstrap;

Does anyone know how to properly config webpack to get it? I'd like to keep using webpack since all projects are using it.

Additional info:


Solution

  • Found out by deeply reading the docs from webpack and debugging master.js in bull package.

    Just add commonjs as library target in webpack config:

    module.exports = {
    output: {
      //filename: ...
      //path: ...
      library: {
        type: 'commonjs-module',
      },
     }
    }
    

    The compiled js file will have this line:

    module.exports = __webpack_exports__;