Bull.js在NestJS中与webpack一起无法获取导出的函数。

huangapple go评论109阅读模式
英文:

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

问题

My NestJS application compiles my Typescript worker into a js file with the following line:

exports["default"] = bootstrap;

但是 Bull.js 报错如下:

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

只有在我手动添加以下内容到编译后的 js 文件中时才能正常工作:

module.exports = bootstrap;

有人知道如何正确配置 webpack 以解决这个问题吗?我想继续使用 webpack,因为所有项目都在使用它。

附加信息:

  • bull 版本:4.10.4;
  • webpack 版本:5.76.3;
  • nestjs 版本:9.3.12;
英文:

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:

  • bull version: 4.10.4;
  • webpack version: 5.76.3;
  • nestjs version: 9.3.12;

答案1

得分: 0

通过深入阅读webpack文档和调试bull包中的master.js文件发现。

只需在webpack配置中将commonjs作为库目标添加:

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

编译后的js文件将包含以下行:

module.exports = __webpack_exports__;
英文:

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__;

huangapple
  • 本文由 发表于 2023年6月15日 00:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定