Giter VIP home page Giter VIP logo

node-source-map-support's Introduction

Source Map Support

This module provides source map support for stack traces in node via the V8 stack trace API. It uses the source-map module to replace the paths and line numbers of source-mapped files with their original paths and line numbers. The output mimics node's stack trace format with the goal of making every compile-to-JS language more of a first-class citizen. Source maps are completely general (not specific to any one language) so you can use source maps with multiple compile-to-JS languages in the same node process.

Installation

npm install source-map-support

This module takes effect globally and should be initialized by inserting require('source-map-support').install() at the top of your code.

Usage

To use a valid source map (generated using something like source-map-index-generator), the compiled code should contain the following two extra lines:

//@sourceMappingURL=path/to/sourceMapFile.json
require('source-map-support').install();

Demos

Basic Demo

original.js:

throw new Error("test");

compiled.js:

//#sourceMappingURL=original.sourcemap.json
require('source-map-support').install();

throw new Error("test");

original.sourcemap.json:

{"version":3,"file":"compiled.js","sources":["original.js"],"names":[],"mappings":";;;AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"}

After running compiled.js you should get the following outout:

/path/to/original.js:1
throw new Error("test");
      ^
Error: test
    at Object.<anonymous> (/path/to/original.js:1:7)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

CoffeeScript Demo

The following terminal commands show a stack trace in node with CoffeeScript filenames:

$ cat > demo.coffee

require('source-map-support').install()
foo = ->
  bar = -> throw new Error 'this is a demo'
  bar()
foo()

$ npm install source-map-support coffee-script
$ node_modules/coffee-script/bin/coffee --map --compile demo.coffee
$ node demo

demo.coffee:4
  bar = -> throw new Error 'this is a demo'
                    ^
Error: this is a demo
    at bar (demo.coffee:4:21)
    at foo (demo.coffee:5:3)
    at Object.<anonymous> (demo.coffee:6)
    at Object.<anonymous> (demo.coffee:2)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

TypeScript Demo

The following terminal commands show a stack trace in node with TypeScript filenames:

$ cat > demo.ts

declare function require(name: string);
require('source-map-support').install();
class Foo {
  constructor() { this.bar(); }
  bar() { throw new Error('this is a demo'); }
}
new Foo();

$ npm install source-map-support typescript
$ node_modules/typescript/bin/tsc -sourcemap demo.ts
$ node demo

demo.ts:6
  bar() { throw new Error('this is a demo'); }
               ^
Error: this is a demo
    at Foo.bar (demo.ts:6:16)
    at new Foo (demo.ts:5:23)
    at Object.<anonymous> (demo.ts:8)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

Options

This module installs two things: a change to the stack property on Error objects and a handler for uncaught exceptions that mimics node's default exception handler (the handler can be seen in the demos above). You may want to disable the handler if you have your own uncaught exception handler. This can be done by passing an argument to the installer:

require('source-map-support').install({
  handleUncaughtExceptions: false
});

License

This code is available under the MIT license.

node-source-map-support's People

Contributors

evanw avatar floridoo avatar fresheneesz avatar gfx avatar michaelficarra avatar zeekay avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.