Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify namespace with url in ESM #948

Open
sebamarynissen opened this issue Oct 19, 2023 · 5 comments
Open

Specify namespace with url in ESM #948

sebamarynissen opened this issue Oct 19, 2023 · 5 comments
Labels
change-minor This proposes or provides a change that requires a minor release feature This proposes or provides a feature or enhancement
Milestone

Comments

@sebamarynissen
Copy link

When using native esm in Node, it's not possible anymore to use the pattern

const debug = require('debug')('namespace');

and we have to do

import createDebug from 'debug';
const debug = createDebug('namespace');

instead. In one of my projects, I've found myself to use an alternative approach though, where I do something like

import createDebug from 'debug';
const url = new URL(import.meta.url);
const [[namespace]] = url.searchParams;
export default createDebug(namespace);

so that it becomes possible to do stuff like

// In server
import debug from './debug.js?server';

// In worker
import debug from './debug.js?worker';

I was wondering whether it would be useful to have this functionality in the library itself. I'd be happy to clean my code up a bit and file a PR for this. An alternative approach would be to create my own npm module on top of the debug module if it's not desirable to have this in the library itself.

For the syntax, I was thinking about something like

import debug from 'debug/url?namespace'

where debug/url can become an exports in the package.json.

The url approach can be used to add more functionality as well, for example to force a specific color or something:

import createDebug from 'debug';
const url = new URL(import.meta.url);
const [namespace] = [...url.searchParams].find(([key, value]) => value === '');
const debug = createDebug(namespace);
const color = url.searchParams.get('color');
if (color !== null) {
  debug.color = color;
}
export default debug;

I'd love to hear some thoughts about this from the maintainers.

@bryanjtc
Copy link

bryanjtc commented Nov 1, 2023

A workaround you can use is createRequire

import { createRequire } from 'node:module';

const newRequire = createRequire(import.meta.url);
const debug('namespace');

@sebamarynissen
Copy link
Author

sebamarynissen commented Nov 1, 2023

@bryanjtc That would be kind of pointless because the entire goal is to make it a one-liner again.

Anyway, I've decided to create my own npm module debug-ns for it. I would be happy to eventually merge it in the debug module and then mark my debug-ns as deprecated.

@Qix-
Copy link
Member

Qix- commented Nov 1, 2023

This actually isn't the worst idea. To be completely honest I didn't know this was possible.

Right now the V5 roadmap is blocked (still) on some Node things, but it'd be nice to have this eventually.

@Qix- Qix- added feature This proposes or provides a feature or enhancement change-minor This proposes or provides a change that requires a minor release labels Nov 1, 2023
@Qix- Qix- added this to the 5.x milestone Nov 1, 2023
@Qix- Qix- mentioned this issue Nov 1, 2023
11 tasks
@sebamarynissen
Copy link
Author

@Qix- Cool, I'll try to file a PR for this soon

@yetzt
Copy link

yetzt commented Mar 1, 2024

@sebamarynissen you could do this in one line:

const debug = (await import("debug")).default("namespace");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change-minor This proposes or provides a change that requires a minor release feature This proposes or provides a feature or enhancement
Development

No branches or pull requests

4 participants