bun
bun copied to clipboard
`debug` package does not work
import debug from "debug";
debug.enable("*");
export const logError = debug("server:mp:error");
export const logWarn = debug("server:mp:warn");
export const log = debug("server:mp:log");
log("hello world");
logWarn("hello world");
logError("hello world");
TypeError: exports.humanize is not a function. (In 'exports.humanize(this.diff)', 'exports.humanize' is undefined)
First reported on Discord: https://discord.com/channels/876711213126520882/1156253242884374549
Hello, is there any solutions or fixes for this ? :)
I get similar problem when build. debug-js in dist index.js will like this
but actually it is
you can use
--external debug to resolve that
I was able to resolve it by passing --external debug to the command line bun build process and that solved it for me.
@videate-josh Can you elaborate? You mean simply adding --external debug to the bun build ... command? Because that leads to:
error: Cannot find package "debug" from "compiled://root/..."
I haven't confirmed, but I would assume this only works if you have debug installed globally or something. If you want to have a truly runtime free executable, I don't think this will work. Or am I missing something?
@mtiller-jh I'm building out my esm module like this:
bun build ./cli.ts --target node --external lodash-es --external debug --outdir ./dist --tsconfig tsconfig.esm.json
So my solution to this was as follows. For my entry point (the thing I'm building), I add this code at the top:
import debug from "debug";
import ms from "ms";
debug.formatArgs = function formatArgs(this: any, args: string[]) {
const { namespace: name, useColors } = this;
if (useColors) {
const c = this.color;
const colorCode = "\u001B[3" + (c < 8 ? c : "8;5;" + c);
const prefix = ` ${colorCode};1m${name} \u001B[0m`;
args[0] = prefix + args[0].split("\n").join("\n" + prefix);
args.push(colorCode + "m+" + ms(this.diff) + "\u001B[0m");
} else {
args[0] = new Date().toISOString() + name + " " + args[0];
}
};
This requires me to add ms as a dependency for my project (but it would be in the bundle anyway). Note, this very deliberately doesn't make the dependency --external because I want everything included. Instead, I just have an alternative way of bringing in the ms dependency (that someone bun figures out, unlike the whatever it is that debug is trying to do).
Net result, coloring work as expected.
Still broken, I think it is something with ESM and CJS compatibility, but does anyone have any guess or something more concrete?
Found https://www.npmjs.com/package/consola to be a great alternative, though not a drop-in replacement, creates loggers with different logging levels. But uses same DEBUG variable and allows using tags same way as debug does.
Simple solution, if you're ok without colors in your logger output (which probably you won't need in a release build), is to set DEBUG_COLORS=false in env when launching app.
this appears to be working now as of at least Bun 1.1.8