2

I am working on a MEAN stack app with Angular 2 frontend. I have successfully used debug in the express app. However I am unable to cleanly import debug in app.components.ts or main.module.ts. Any idea on how to proceed?

  • <script src="/node_modules/debug/debug.js"></script> Results in error: Uncaught ReferenceError: module is not defined
  • <script>System.import('./node_modules/debug/debug.js');</script> Not good either: zone.js:101 GET http://localhost:8002/ms.js 404 (Not Found) Some dependency script not able to load.
  • import { debug } from '../../../node_modules/debug/debug.js'; inside any app file also gives error: zone.js:101 GET http://localhost:8002/ms.js 404 (Not Found); (index):25 Error: Error: XHR error (404 Not Found) loading http://localhost:8002/ms.js(…)

Solved

Finally solved it thanks to @candidJ. I highly recommend using this tool to debug your app. After finishing development, refactor all your console.log() statements into debugApp() instead of deleting them. Remember, you can namespace them per module, and enable/disable them individually or en-mass. These will prove very useful later on for other developers backtracking trough the code for maintenance or debugging.

artem
  • 46,476
  • 8
  • 74
  • 78
Adrian Moisa
  • 3,923
  • 7
  • 41
  • 66

2 Answers2

2

you need to have typings installed for debug library inorder to use it in ts file. typings manage your typescript defenitions.

Typings is the simple way to manage and install TypeScript definitions.

here how you can do it :

# Install Typings CLI utility.
  npm install typings --global    

# Find a definition by name.
typings search --name debug

# If you use the package as a module:
# Install non-global typings (defaults to "npm" source, configurable through `defaultSource` in `.typingsrc`).

typings install debug --save

then you can try importing it in your index.html (globally) using either way (as you have done):

  • <script src="/node_modules/debug/debug.js"></script>

  • <script>System.import('./node_modules/debug/debug.js');</script>

for more info on typings look : https://github.com/typings/typings

candidJ
  • 4,289
  • 1
  • 22
  • 32
  • I have installed typings as recommended (globals and modules) and I'm still getting those errors. In `tsconfig.json` I have added `"compilerOptions": { "types": [ "core-js", "debug"], ...}.` When I run in cmd `npm run tsc -w` I get the following message: `error TS2688: Cannot find type definition file for 'debug'.` I'm using typescript 2.0.3. – Adrian Moisa Sep 23 '16 at 13:13
  • 1
    Thank you for the hint! It helped me figure out what to do next. I finally managed to print messages in console. Check my [answer](http://stackoverflow.com/a/39666118/1903781) for specific details. – Adrian Moisa Sep 23 '16 at 17:09
  • just read your edited question. try using `this.debugApp = debug('app'); this.debugApp('test');` in constructor method of your component. I haven't worked with `debug.js` earlier. – candidJ Sep 23 '16 at 17:09
  • 1
    @AdrianMoisa you eventually figured it out on your own. I learned a lot from this question :) – candidJ Sep 23 '16 at 17:13
  • **Update:** [`typings` has been deprecated.](https://github.com/typings/typings) There are instructions on how to replace it, over in [its README](https://github.com/typings/typings) – floer32 Mar 17 '21 at 02:28
2

I took some inspiration from Importing lodash into angular2 + typescript application and finally figured out how to import it. No console errors and no compiler errors this time.

First I should mention: when upgrading from typescript 1 to typescript 2 the typings tool gets deprecated. Instead, npm package manager is used to install type definitions.

I followed these steps:

  • npm install debug --save
  • npm install @types/debug --save
  • Mapped debug in system.config.js

system.config.js:

map: {
    'debug': '../node_modules/debug/browser.js',
    ...
}
  • Import in any .ts file you need: import * as debug from 'debug';
  • Optionally, if needed in index.html use: <script>System.import('debug');</script>

Untill now this should work, however a pesky error persists: GET http://localhost:8002/ms.js 404 (Not Found). I fixed this by editing node_modules/debug/debug.js.

  • Replace line 14: exports.humanize = require('ms'); with exports.humanize = require('node_modules/ms/index');.

I am not sure what this implies for other use cases of debug. If you have any suggestions on how to improve this solution so it is not necessary to edit inside node_modules/debug/debug.js write a comment.

Usage in browser

Preferably in app.component.ts or main.module.ts write:

// Expose debugsApp in window object in order to access it from the console. 
// window.debug is already used by chrome. 
import * as debug from 'debug';
(<any>window).debugApp = debug; // The typescript way to extend window

In any other .ts file:

import * as Debug from 'debug';

var debug = Debug('app:someModule');
debug('Some message');

Finally in the console type as you need:

// Business as usual
debugApp.enable('*'); // Enable all
debugApp.enable('app'); // Enable app debugger
debugApp.enable('app:*'); // Enable app debuggers if they are namespaced
debugApp.enable('app:someModule'); // Enable someModule
debugApp.disable('*'); // Disable all

EDIT

I had an unexpected issue with this method. I could either load the server path or the frontend path for the debug script. So I had to do another improvisation.

node_modules/debug/debug.js - Line 14

if (typeof process === 'undefined') {
  exports.humanize = require('node_modules/ms/index.js');
} else {
  exports.humanize = require('ms');
}

This triggers another issue on itself. System.js likes to parse ahead the exports so this leads to abnormal behavior when exports statement is combined with an if statement. More details here. Fortunately there is a fix. More details here thanks to @guybedford

In system.config.js add:

System.config({
    map: {
        ms: '@empty'
    }
});

In the end it's a patch-up job, but it works. Hopefully debug authors will suggest a better solution. Until then use this.

Community
  • 1
  • 1
Adrian Moisa
  • 3,923
  • 7
  • 41
  • 66