bun icon indicating copy to clipboard operation
bun copied to clipboard

`debug` package does not work

Open Electroid opened this issue 2 years ago • 9 comments

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

Electroid avatar Sep 26 '23 16:09 Electroid

Hello, is there any solutions or fixes for this ? :)

hpapier avatar Dec 11 '23 09:12 hpapier

I get similar problem when build. debug-js in dist index.js will like this image but actually it is image you can use --external debug to resolve that

weifpeng avatar Jan 04 '24 09:01 weifpeng

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 avatar Feb 06 '24 00:02 videate-josh

@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 avatar Feb 28 '24 19:02 mtiller-jh

@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

videate-josh avatar Feb 28 '24 19:02 videate-josh

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.

mtiller-jh avatar Feb 28 '24 20:02 mtiller-jh

Still broken, I think it is something with ESM and CJS compatibility, but does anyone have any guess or something more concrete?

msanchezdev avatar Apr 03 '24 04:04 msanchezdev

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.

msanchezdev avatar Apr 03 '24 04:04 msanchezdev

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.

marekpiechut avatar May 05 '24 19:05 marekpiechut

image

this appears to be working now as of at least Bun 1.1.8

nektro avatar May 17 '24 03:05 nektro