How to use the "debug" module with typescript
Asked Answered
D

3

29

I have an NTVS (Node Tools for Visual Studio) project with Typescript.
The following statement doesn't compile:

import debug = require('debug')('MyApp');

The syntax error being

(TS) ';' expected

between the the two parenthesis ')('
Is it possible to use "debug" with TypeScript?

Driest answered 9/12, 2017 at 0:47 Comment(1)
Nothing here worked but finally got it working using this easy approach ... stackoverflow.com/a/75757692/1205871Aribold
H
52

From the README, debug module is exporting a function that decorates console.error with your module name (MyApp). I'm guessing there are other ways, but I use:

import Debug from "debug";
const debug = Debug("MyApp");

// then to use
debug("Something happened");

And to print everything to the console, run your app with...

$ DEBUG=* node MyApp.js
Halla answered 17/1, 2018 at 21:11 Comment(3)
import * as Debug from "debug"; can be turned into a default import statement import Debug from "debug";Gameto
I only got it working by by using the remark by @AndyFaizan. import * as Debug from "debug" didn't seem to work (VS Code underlined it in red). Using TypeScript 3.Sheliasheline
Doesn't work with TS4 latest Angular - I give up with npm debug - time to write my own logging funcAribold
R
28

The answers here did not work for me with more recent versions of Typescript. Here's how I got it working with proper import syntax in Typescript ^3.5.3:

Install Debug package and Typescript types for Debug (types only needed for dev)

npm install --save debug
npm install --save-dev @types/debug

Then in .ts files:

import Debug from "debug";
const debug = Debug("AppName");

Hope this helps someone else!

Rodneyrodolfo answered 10/7, 2019 at 0:22 Comment(4)
npm install -D --save-dev @types/debug is better.Blakemore
-D and --save-dev are the -exact- same parameters, -D is just shorthand and I prefer --save-dev longhand for exactly this reason; easier to remember.Rodneyrodolfo
I was using scripts to run the node app. I change my start script "export DEBUG=* && nodemon index.ts" and kinda forgot that I have to restart for the changes to take effect. Leaving this here if anyone else runs into the same issueContemptible
Doesn't work with TS4 latest Angular - I give up with npm debug - time to write my own logging funcAribold
H
1

Remember that TypeScript is a super-set of javascript, so you can still also do this one-liner:

const debug = require('debug')('my-app:my-module');

Typescript seems to conclude that the debug constant here is of type 'any', and you lose all type safety, but with a package as simple as debug is, I think you will be OK...

Personally, I think 2 lines to instantiate debugging in every module is 1 line too many, so I continue to use this one-liner in my .ts files.

P.S. I like to use module tags so I can enable logging in just certain modules with DEBUG=my-app:my-module,my-app:some-other-module ts-node my-app or all my modules with DEBUG=my-app:* ...

Havre answered 10/3, 2018 at 14:14 Comment(1)
I should say - 5 years on and I have been converted to Typescript more, and would probably now see this as dirty in a Typescript file... Each to their own...Havre

© 2022 - 2024 — McMap. All rights reserved.