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?
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 .
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.
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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With