27

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?

Nicolae Daian
  • 1,065
  • 3
  • 18
  • 39
  • Nothing here worked but finally got it working using this easy approach ... https://stackoverflow.com/a/75757692/1205871 – danday74 Mar 16 '23 at 14:39

4 Answers4

46

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
Rick
  • 3,240
  • 2
  • 29
  • 53
bjacobowski
  • 789
  • 6
  • 6
  • 11
    `import * as Debug from "debug";` can be turned into a default import statement `import Debug from "debug";` – AndyFaizan Sep 27 '18 at 09:46
  • 4
    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. – Peter Nov 13 '18 at 14:48
  • Doesn't work with TS4 latest Angular - I give up with npm debug - time to write my own logging func – danday74 Jan 29 '21 at 10:53
26

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!

Scott Byers
  • 3,007
  • 2
  • 17
  • 14
  • `npm install -D --save-dev @types/debug` is better. – Hill May 14 '20 at 08:52
  • 5
    `-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. – Scott Byers May 15 '20 at 11:54
  • 2
    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 issue – Abdelrahman Shoman Jan 20 '21 at 13:46
  • Doesn't work with TS4 latest Angular - I give up with npm debug - time to write my own logging func – danday74 Jan 29 '21 at 10:52
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:* ...

spechter
  • 2,058
  • 1
  • 17
  • 23
  • 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... – spechter Feb 21 '23 at 00:27
0

The solution to debug not showing any logs in typescript is due to the reason that debug relies on the environment variables to decide how to show the logs

Solution

Make sure you have installed dotenv and its type definition file

npm install dotenv && npm install -D @types/dotenv

Then create a .env file at the root folder of your project add and this environment variable:

DEBUG = *

finally at the index file of your application. Configure dotenv to load the environment variables before any other task is run.

Its very important dotenv configuration is done at the top of the index file, before any other lines of code.

Add this two lines of code

import dotenv from "dotenv";
dotenv.config();

This should load the DEBUG environment variable required by debug to show output on the standard input.

Remember if you want to view logs defined in your files only and not other logs from other modules. Its better you define your application name as the namespace. That way you can filter the logs by the environment variable described above i.e.

const debug = debug("applicationName:other-more-information");

Then to view your debug logs alone just change the DEBUG varibale from * to applicationName:*

DEBUG = applicationName:*

For unix based os users you can try exporting this variable to enviroment variables directly (Though i have not tested this solution) export DEBUG=* - This method will only work for all process started on this shell

Andrew Mititi
  • 933
  • 12
  • 11