how can I use npm "debug" package with es modules

Viewed 774

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
1 Answers

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.

Related