Bull: Problem with using with typescript builded with webpack

Created on 11 Dec 2017  路  8Comments  路  Source: OptimalBits/bull

Hello

I have a problem using bull with typescript. I build typescript using webpack.
Bull is working as expected when using with js.
Proof sample located here.

Hope for your help.
Thanks

cannot reproduce

Most helpful comment

But why are you using webpack? you should be fine just using the typescript compiler directly...

@manast Hot Module Reloading

Why did you add a cannot reproduce label and closed this issue? The OP is providing a reproduction repository. I am also experiencing this issue in a setup with webpack and bull.

I digged into it a bit deeper and the issue is that __dirname is used which will be different because webpack has a different behaviour. There are multiple solutions to this problem:

  1. Add option to define the lua script directory via a environment variable which overrides the usage of __dirname
  2. Inline lua files as template literals (could also be done as part of a build step using babel.macros, but it seems like this project is not using babel)
  3. Replace __dirname with the correct path with webpack

@aliksend I did the latter with the string-replace-loader:

webpack.config.js

const path = require("path");

module.exports = {
  ...yourConfig,
  module: {
    rules: [
      {
        test: /node_modules\/bull\/lib\/commands\/index\.js$/,
        use: {
          loader: "string-replace-loader",
          options: {
            search: "__dirname",
            replace: `"${path.dirname(require.resolve("bull"))}/lib/commands"`
          }
        }
      }
    ]
  }
};

All 8 comments

sorry, I have no idea what the error can be, seems related to redis custom commands. But why are you using webpack? you should be fine just using the typescript compiler directly...

I develop a vue application with server-side rendering and for me there is only one way to be sure that client and server parts compiles the same way. Sure, I can build client part with webpack and run server part directly (using ts-node etc) but then I lose all webpack settings such as DefinePlugin, resolve.alias, also there was some problems with importing etc. I played with it some time ago and got one working way - to compile both parts with webpack. And this issue is the first problem because of this.

But why are you using webpack? you should be fine just using the typescript compiler directly...

@manast Hot Module Reloading

Why did you add a cannot reproduce label and closed this issue? The OP is providing a reproduction repository. I am also experiencing this issue in a setup with webpack and bull.

I digged into it a bit deeper and the issue is that __dirname is used which will be different because webpack has a different behaviour. There are multiple solutions to this problem:

  1. Add option to define the lua script directory via a environment variable which overrides the usage of __dirname
  2. Inline lua files as template literals (could also be done as part of a build step using babel.macros, but it seems like this project is not using babel)
  3. Replace __dirname with the correct path with webpack

@aliksend I did the latter with the string-replace-loader:

webpack.config.js

const path = require("path");

module.exports = {
  ...yourConfig,
  module: {
    rules: [
      {
        test: /node_modules\/bull\/lib\/commands\/index\.js$/,
        use: {
          loader: "string-replace-loader",
          options: {
            search: "__dirname",
            replace: `"${path.dirname(require.resolve("bull"))}/lib/commands"`
          }
        }
      }
    ]
  }
};

I encountered the same issue when using serverless-webpack. I am attempting to add a job to queue via a lambda in a Serverless framework setup.

The solution mentioned by @n1ru4l works. Hopefully, we can find a way to make it work without this fix.

@n1ru4l solution works for me. Thanks!

@n1ru4l solution also worked for me, thanks.

This is a duplicate of #920

None of the solutions I saw posted worked for me. However, I was able to get this working by changing the __dirname resolution behavior:

node: {
    // required for __dirname to properly resolve
    // Also required for `bull` to work, see https://github.com/OptimalBits/bull/issues/811
    __dirname: true,
},

See this for a bit more context

@n1ru4l's solution works only if bundle is near node_modules. I'm configuring flow with tiny docker images and single bundle for my nestjs app in nx workspace.

So I just put lua files at same folder with my main.js. This looks like not so cool workaround but it is quick and effitiend in this case.
Here is example:

// angular.json


["app.architect.build.options.assets"]: [
  "apps/routing-worker/src/assets",
  {
    "glob": "**/*.lua",
    "input": "node_modules/bull/lib/commands",
    "output": "./"
  }
]

You can place everywhere you prefer by changing output and using @n1ru4l's solution to replace __dirname.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rodrigoords picture rodrigoords  路  4Comments

davedbase picture davedbase  路  3Comments

inn0vative1 picture inn0vative1  路  4Comments

thelinuxlich picture thelinuxlich  路  3Comments

pigaov10 picture pigaov10  路  3Comments