BullMQ fails to build with Bun
What version of Bun is running?
v1.0.22
What platform is your computer?
Darwin 22.6.0 arm64 arm
What steps can reproduce the bug?
- install BullMQ
- Add a bull queue and dummy worker that console.logs in the
index.ts - build the index.ts using
bun build --target bun --outdir build - run
bun build/index.ts
error
function getPkgJsonDir() {
for (const modPath of module.paths || []) {
^
ReferenceError: Can't find variable: module
What is the expected behavior?
No response
What do you see instead?
No response
Additional information
No response
Using --compile I get:
ReferenceError: Can't find variable: module_script_loader
at getPkgJsonDir (/$bunfs/root/cli:46442:25)
at new ScriptLoader (/$bunfs/root/cli:46509:21)
at /$bunfs/root/cli:46722:20
I'm getting the same error from a bundled BullMQ server script. Running the TS file directly works fine, but building (with target bun) and then running it throws ReferenceError: Can't find variable: module_script_loader.
Seems to be caused by this function, which gets bundled to this:
var getPkgJsonDir = function() {
for (const modPath of module_script_loader.paths || []) {
try {
const prospectivePkgJsonDir = path3.dirname(modPath);
fs2.accessSync(modPath, fs2.constants.F_OK);
return prospectivePkgJsonDir;
} catch (e) {
}
}
return "";
};
Same error
Any news on this ?
I ran into this error too, the BullMQ method that @Curetix mentions seems to hold a clue; the method's comment references a discussion on stack overflow which states;
Node publishes all the module search paths to module.paths. We can traverse these and pick the first one that resolves.
It would appear that Bun follows this behavior only when running. When building/compiling the 'module' global seems to be replaced somehow, which suggests to me it may not be recognized or set as a global.
A simple test file confirms this;
// index.ts
console.log(module.paths);
When launched with bun run this will produce an array output.
When built via bun build --target bun --outfile out.js index.ts and subsequently run via bun run out.js this produces undefined.
@knajjars I suggest you change the title of your bug report so to something like module.paths not set when using bun build - surely there will be modules other than BullMQ suffering from this bug.
EDIT: There seems to be a bit more to it; apparently this is caused by BullMQ using the module.paths to dynamically load Lua files onto the Redis instance, which would hide these files from the Bun bundler. These would need to be turned into assets by the bundler somehow.
Hey! Same problem here. Did anyone found a solution?
Hello everyone! I encountered the same problem, and I'm still not sure if this is the best solution. However, this is what worked for me using Bun with SandboxedJob, after reviewing the source code.
// worker.ts
import { ParentCommand, type SandboxedJob } from "bullmq";
export default async function(job: SandboxedJob) {
console.log(`Job ${job.name}:${job.id} started with data`, job.data);
// return value
exit({
sent: true,
});
}
function exit<T extends any>(data: T, success: boolean = true) {
process.send?.({
cmd: success ? ParentCommand.Completed : ParentCommand.Failed,
data: data
});
process.exit(0);
}
This way, the jobs are no longer marked as failed. But again, I'm not sure if it's the best solution.