bullmq: Vercel add Job : TypeError: client.addJob is not a function

Thanks for developing this open source. I really enjoy it. I have encountered this problem when running on vercel serverless. The source code works perfectly fine on local.

{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "TypeError: client.addJob is not a function",
  "reason": {
    "errorType": "TypeError",
    "errorMessage": "client.addJob is not a function",
    "stack": [
      "TypeError: client.addJob is not a function",
      " at Function.addJob (/var/task/node_modules/bullmq/dist/classes/scripts.js:52:23)",
      " at Job.addJob (/var/task/node_modules/bullmq/dist/classes/job.js:352:34)",
      " at Function.create (/var/task/node_modules/bullmq/dist/classes/job.js:34:28)",
      " at processTicksAndRejections (internal/process/task_queues.js:93:5)",
      " at Queue.add (/var/task/node_modules/bullmq/dist/classes/queue.js:38:25)"
    ]
  },
  "promise": {},
  "stack": [
    "Runtime.UnhandledPromiseRejection: TypeError: client.addJob is not a function",
    " at process.<anonymous> (/var/runtime/index.js:35:15)",
    " at process.emit (events.js:327:22)",
    " at process.emit (/var/task/___vc_sourcemap_support.js:587:21)",
    " at processPromiseRejections (internal/process/promises.js:245:33)",
    " at processTicksAndRejections (internal/process/task_queues.js:94:32)"
  ]
}

Example Code:

const connection = new IORedis(process.env.REDIS_URI);
const queue = new Queue('SENDEMAIL', { connection });
const job = await queue.add('SEND',  { email });

I found a workaround for this while customizing the vercel.json file. Hope to be able to help anyone who is having this problem.

    "functions": {
        "api/serverless.ts": {
            "includeFiles": "node_modules/bullmq/dist/commands/**",
        }
    },

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 34 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Upstash is going to be compatible very soon, when they support Redis Streams which is almost ready.

When a bundler runs (ie webpack, rollup, esbuild), either directly or as Vercel must also be doing in the background, it cannot handle / resolve circular dependencies so it creates these orphaned stubs that register as undefined at runtime.

As an example of some on that list, these are the root cause: https://github.com/taskforcesh/bullmq/blob/master/src/classes/job.ts#L6 https://github.com/taskforcesh/bullmq/blob/master/src/classes/queue-scheduler.ts#L3 https://github.com/taskforcesh/bullmq/blob/master/src/classes/repeat.ts#L3 https://github.com/taskforcesh/bullmq/blob/master/src/classes/queue.ts#L5 https://github.com/taskforcesh/bullmq/blob/master/src/classes/worker.ts#L5

(In fact, looking at that list, the above should solve most of them).

If interested in making sure the package is able to be bundled, I have found it super useful to run madge in the CI build loop to immediately detect problems. ie

{
  "scripts": {
    "madge": "madge --orphans -j dist/index.js && madge --circular -j dist/index.js"
   }
}

or

      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          yarn build
          npx madge --orphans -j dist/index.js && madge --circular -j dist/index.js
          npx semantic-release

I found a workaround for this while customizing the vercel.json file. Hope to be able to help anyone who is having this problem.

    "functions": {
        "api/serverless.ts": {
            "includeFiles": "node_modules/bullmq/dist/commands/**",
        }
    },

@ctrlaltdylan any chances to report this issue to the Vercel team?

I had the same issue with esbuild. I solved it by declaring bullmq as external esbuild --external:bullmq

I do not know how vercel works but my feeling is that the deployment is missing the lua files: https://github.com/taskforcesh/bullmq/tree/master/src/commands which also is placed on the dist directory of the npm module at /bullmq/dist/commands If those lua scripts are in the correct place when any of BullMQ main classes are instantiated they will be loaded and you should not get any error.