how can I use npm "debug" package with es modules
Asked Answered
G

1

12

this is how I used to used it in common modules.

const debuger=require('debug')("namespace")

I set an environmental variable DEBUG="namespace" and when i start the app, i can use the debugger.

However I could not figure out how to use it with import/export staments.

  import debugger from "debug" // how can i pass () here
Goldstone answered 24/2, 2021 at 3:26 Comment(1)
debugger is a reserved keyword, so if you changed it to import debug or really anything else, it will workSolander
L
15

You can do:

import debug from 'debug';
const logger = debug('namespace');
logger('Starting App');

Actually, as the npm debug module exports a function directly (module.exports = (params) => {...), you can give the function whatever name you like, for example:

import createDebugMessages from 'debug';
const debug = createDebugMessages('namespace');
debug('Starting App');

This way, the original syntax for debugging does not even need to be changed.

Liberalism answered 15/6, 2021 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.