Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the "debug" module with typescript

Tags:

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?

like image 718
Nicolae Daian Avatar asked Dec 09 '17 00:12

Nicolae Daian


People also ask

How do I run TypeScript in debug mode?

Depending on the way you specified your TypeScript file in the run/debug configuration, do one of the following: If you typed the filename explicitly, select the file in the Project tool window or open it in the editor and then select Run <run/debug configuration>. next to the list or press Shift+F10 .

How do I debug a npm module?

debug exposes a function; simply pass this function the name of your module, and it will return a decorated version of console. error for you to pass debug statements to. This will allow you to toggle the debug output for different parts of your module as well as the module as a whole. Example app.


2 Answers

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 
like image 54
bjacobowski Avatar answered Oct 11 '22 01:10

bjacobowski


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!

like image 44
Scott Byers Avatar answered Oct 11 '22 01:10

Scott Byers