\href{https://travis-ci.org/evanw/node-source-map-support}{\texttt{ }}

This module provides source map support for stack traces in node via the \href{https://github.com/v8/v8/wiki/Stack-Trace-API}{\texttt{ V8 stack trace API}}. It uses the \href{https://github.com/mozilla/source-map}{\texttt{ 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\textquotesingle{}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.\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28555}{}\doxysection{Installation and Usage}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28555}
\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28556}{}\doxysubsubsection{Node support}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28556}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install source-\/map-\/support }

\end{DoxyCode}


Source maps can be generated using libraries such as \href{https://github.com/twolfson/source-map-index-generator}{\texttt{ source-\/map-\/index-\/generator}}. Once you have a valid source map, place a source mapping comment somewhere in the file (usually done automatically or with an option by your transpiler)\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{//\# sourceMappingURL=path/to/source.map }

\end{DoxyCode}


If multiple source\+Mapping\+URL comments exist in one file, the last source\+Mapping\+URL comment will be respected (e.\+g. if a file mentions the comment in code, or went through multiple transpilers). The path should either be absolute or relative to the compiled file.

From here you have two options.\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28557}{}\doxyparagraph{CLI Usage}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28557}

\begin{DoxyCode}{0}
\DoxyCodeLine{node -\/r source-\/map-\/support/register compiled.js }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28558}{}\doxyparagraph{Programmatic Usage}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28558}
Put the following line at the top of the compiled file.


\begin{DoxyCode}{0}
\DoxyCodeLine{require('source-\/map-\/support').install(); }

\end{DoxyCode}


It is also possible to install the source map support directly by requiring the {\ttfamily register} module which can be handy with ES6\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{import 'source-\/map-\/support/register'}
\DoxyCodeLine{}
\DoxyCodeLine{// Instead of:}
\DoxyCodeLine{import sourceMapSupport from 'source-\/map-\/support'}
\DoxyCodeLine{sourceMapSupport.install() }

\end{DoxyCode}
 Note\+: if you\textquotesingle{}re using babel-\/register, it includes source-\/map-\/support already.

It is also very useful with Mocha\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ mocha -\/-\/require source-\/map-\/support/register tests/ }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28559}{}\doxysubsubsection{Browser support}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28559}
This library also works in Chrome. While the Dev\+Tools console already supports source maps, the V8 engine doesn\textquotesingle{}t and {\ttfamily Error.\+prototype.\+stack} will be incorrect without this library. Everything will just work if you deploy your source files using \href{http://browserify.org/}{\texttt{ browserify}}. Just make sure to pass the {\ttfamily -\/-\/debug} flag to the browserify command so your source maps are included in the bundled code.

This library also works if you use another build process or just include the source files directly. In this case, include the file {\ttfamily browser-\/source-\/map-\/support.\+js} in your page and call {\ttfamily source\+Map\+Support.\+install()}. It contains the whole library already bundled for the browser using browserify.


\begin{DoxyCode}{0}
\DoxyCodeLine{<script src="{}browser-\/source-\/map-\/support.js"{}></script>}
\DoxyCodeLine{<script>sourceMapSupport.install();</script> }

\end{DoxyCode}


This library also works if you use AMD (Asynchronous Module Definition), which is used in tools like \href{http://requirejs.org/}{\texttt{ Require\+JS}}. Just list {\ttfamily browser-\/source-\/map-\/support} as a dependency\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{<script>}
\DoxyCodeLine{  define(['browser-\/source-\/map-\/support'], function(sourceMapSupport) \{}
\DoxyCodeLine{    sourceMapSupport.install();}
\DoxyCodeLine{  \});}
\DoxyCodeLine{</script> }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28560}{}\doxysection{Options}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28560}
This module installs two things\+: a change to the {\ttfamily stack} property on {\ttfamily Error} objects and a handler for uncaught exceptions that mimics node\textquotesingle{}s default exception handler (the handler can be seen in the demos below). 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\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{require('source-\/map-\/support').install(\{}
\DoxyCodeLine{  handleUncaughtExceptions: false}
\DoxyCodeLine{\}); }

\end{DoxyCode}


This module loads source maps from the filesystem by default. You can provide alternate loading behavior through a callback as shown below. For example, \href{https://github.com/meteor}{\texttt{ Meteor}} keeps all source maps cached in memory to avoid disk access.


\begin{DoxyCode}{0}
\DoxyCodeLine{require('source-\/map-\/support').install(\{}
\DoxyCodeLine{  retrieveSourceMap: function(source) \{}
\DoxyCodeLine{    if (source === 'compiled.js') \{}
\DoxyCodeLine{      return \{}
\DoxyCodeLine{        url: 'original.js',}
\DoxyCodeLine{        map: fs.readFileSync('compiled.js.map', 'utf8')}
\DoxyCodeLine{      \};}
\DoxyCodeLine{    \}}
\DoxyCodeLine{    return null;}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}); }

\end{DoxyCode}


The module will by default assume a browser environment if XMLHttp\+Request and window are defined. If either of these do not exist it will instead assume a node environment. In some rare cases, e.\+g. when running a browser emulation and where both variables are also set, you can explictly specify the environment to be either \textquotesingle{}browser\textquotesingle{} or \textquotesingle{}node\textquotesingle{}.


\begin{DoxyCode}{0}
\DoxyCodeLine{require('source-\/map-\/support').install(\{}
\DoxyCodeLine{  environment: 'node'}
\DoxyCodeLine{\}); }

\end{DoxyCode}


To support files with inline source maps, the {\ttfamily hook\+Require} options can be specified, which will monitor all source files for inline source maps.


\begin{DoxyCode}{0}
\DoxyCodeLine{require('source-\/map-\/support').install(\{}
\DoxyCodeLine{  hookRequire: true}
\DoxyCodeLine{\}); }

\end{DoxyCode}


This monkey patches the {\ttfamily require} module loading chain, so is not enabled by default and is not recommended for any sort of production usage.\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28561}{}\doxysection{Demos}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28561}
\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28562}{}\doxysubsubsection{Basic Demo}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28562}
original.\+js\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{throw new Error('test'); // This is the original code }

\end{DoxyCode}


compiled.\+js\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{require('source-\/map-\/support').install();}
\DoxyCodeLine{}
\DoxyCodeLine{throw new Error('test'); // This is the compiled code}
\DoxyCodeLine{// The next line defines the sourceMapping.}
\DoxyCodeLine{//\# sourceMappingURL=compiled.js.map }

\end{DoxyCode}


compiled.\+js.\+map\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  "{}version"{}: 3,}
\DoxyCodeLine{  "{}file"{}: "{}compiled.js"{},}
\DoxyCodeLine{  "{}sources"{}: ["{}original.js"{}],}
\DoxyCodeLine{  "{}names"{}: [],}
\DoxyCodeLine{  "{}mappings"{}: "{};;AAAA,MAAM,IAAI"{}}
\DoxyCodeLine{\} }

\end{DoxyCode}


Run compiled.\+js using node (notice how the stack trace uses original.\+js instead of compiled.\+js)\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ node compiled.js}
\DoxyCodeLine{}
\DoxyCodeLine{original.js:1}
\DoxyCodeLine{throw new Error('test'); // This is the original code}
\DoxyCodeLine{      \string^}
\DoxyCodeLine{Error: test}
\DoxyCodeLine{    at Object.<anonymous> (original.js:1:7)}
\DoxyCodeLine{    at Module.\_compile (module.js:456:26)}
\DoxyCodeLine{    at Object.Module.\_extensions..js (module.js:474:10)}
\DoxyCodeLine{    at Module.load (module.js:356:32)}
\DoxyCodeLine{    at Function.Module.\_load (module.js:312:12)}
\DoxyCodeLine{    at Function.Module.runMain (module.js:497:10)}
\DoxyCodeLine{    at startup (node.js:119:16)}
\DoxyCodeLine{    at node.js:901:3 }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28563}{}\doxysubsubsection{Type\+Script Demo}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28563}
demo.\+ts\+:


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

\end{DoxyCode}


Compile and run the file using the Type\+Script compiler from the terminal\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install source-\/map-\/support typescript}
\DoxyCodeLine{\$ node\_modules/typescript/bin/tsc -\/sourcemap demo.ts}
\DoxyCodeLine{\$ node demo.js}
\DoxyCodeLine{}
\DoxyCodeLine{demo.ts:5}
\DoxyCodeLine{  bar() \{ throw new Error('this is a demo'); \}}
\DoxyCodeLine{                \string^}
\DoxyCodeLine{Error: this is a demo}
\DoxyCodeLine{    at Foo.bar (demo.ts:5:17)}
\DoxyCodeLine{    at new Foo (demo.ts:4:24)}
\DoxyCodeLine{    at Object.<anonymous> (demo.ts:7:1)}
\DoxyCodeLine{    at Module.\_compile (module.js:456:26)}
\DoxyCodeLine{    at Object.Module.\_extensions..js (module.js:474:10)}
\DoxyCodeLine{    at Module.load (module.js:356:32)}
\DoxyCodeLine{    at Function.Module.\_load (module.js:312:12)}
\DoxyCodeLine{    at Function.Module.runMain (module.js:497:10)}
\DoxyCodeLine{    at startup (node.js:119:16)}
\DoxyCodeLine{    at node.js:901:3 }

\end{DoxyCode}


There is also the option to use {\ttfamily -\/r source-\/map-\/support/register} with typescript, without the need add the `require(\textquotesingle{}source-\/map-\/support').install()\`{} in the code base\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install source-\/map-\/support typescript}
\DoxyCodeLine{\$ node\_modules/typescript/bin/tsc  -\/sourcemap demo.ts}
\DoxyCodeLine{\$ node -\/r source-\/map-\/support/register demo.js}
\DoxyCodeLine{}
\DoxyCodeLine{demo.ts:5}
\DoxyCodeLine{  bar() \{ throw new Error('this is a demo'); \}}
\DoxyCodeLine{                \string^}
\DoxyCodeLine{Error: this is a demo}
\DoxyCodeLine{    at Foo.bar (demo.ts:5:17)}
\DoxyCodeLine{    at new Foo (demo.ts:4:24)}
\DoxyCodeLine{    at Object.<anonymous> (demo.ts:7:1)}
\DoxyCodeLine{    at Module.\_compile (module.js:456:26)}
\DoxyCodeLine{    at Object.Module.\_extensions..js (module.js:474:10)}
\DoxyCodeLine{    at Module.load (module.js:356:32)}
\DoxyCodeLine{    at Function.Module.\_load (module.js:312:12)}
\DoxyCodeLine{    at Function.Module.runMain (module.js:497:10)}
\DoxyCodeLine{    at startup (node.js:119:16)}
\DoxyCodeLine{    at node.js:901:3 }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28564}{}\doxysubsubsection{Coffee\+Script Demo}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28564}
demo.\+coffee\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{require('source-\/map-\/support').install()}
\DoxyCodeLine{foo = -\/>}
\DoxyCodeLine{  bar = -\/> throw new Error 'this is a demo'}
\DoxyCodeLine{  bar()}
\DoxyCodeLine{foo() }

\end{DoxyCode}


Compile and run the file using the Coffee\+Script compiler from the terminal\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install source-\/map-\/support coffeescript}
\DoxyCodeLine{\$ node\_modules/.bin/coffee -\/-\/map -\/-\/compile demo.coffee}
\DoxyCodeLine{\$ node demo.js}
\DoxyCodeLine{}
\DoxyCodeLine{demo.coffee:3}
\DoxyCodeLine{  bar = -\/> throw new Error 'this is a demo'}
\DoxyCodeLine{                     \string^}
\DoxyCodeLine{Error: this is a demo}
\DoxyCodeLine{    at bar (demo.coffee:3:22)}
\DoxyCodeLine{    at foo (demo.coffee:4:3)}
\DoxyCodeLine{    at Object.<anonymous> (demo.coffee:5:1)}
\DoxyCodeLine{    at Object.<anonymous> (demo.coffee:1:1)}
\DoxyCodeLine{    at Module.\_compile (module.js:456:26)}
\DoxyCodeLine{    at Object.Module.\_extensions..js (module.js:474:10)}
\DoxyCodeLine{    at Module.load (module.js:356:32)}
\DoxyCodeLine{    at Function.Module.\_load (module.js:312:12)}
\DoxyCodeLine{    at Function.Module.runMain (module.js:497:10)}
\DoxyCodeLine{    at startup (node.js:119:16) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28565}{}\doxysection{Tests}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28565}
This repo contains both automated tests for node and manual tests for the browser. The automated tests can be run using mocha (type {\ttfamily mocha} in the root directory). To run the manual tests\+:


\begin{DoxyItemize}
\item Build the tests using {\ttfamily build.\+js}
\item Launch the HTTP server ({\ttfamily npm run serve-\/tests}) and visit
\begin{DoxyItemize}
\item \href{http://127.0.0.1:1336/amd-test}{\texttt{ http\+://127.\+0.\+0.\+1\+:1336/amd-\/test}}
\item \href{http://127.0.0.1:1336/browser-test}{\texttt{ http\+://127.\+0.\+0.\+1\+:1336/browser-\/test}}
\item \href{http://127.0.0.1:1336/browserify-test}{\texttt{ http\+://127.\+0.\+0.\+1\+:1336/browserify-\/test}} -\/ {\bfseries{Currently not working}} due to a bug with browserify (see \href{https://github.com/evanw/node-source-map-support/pull/66}{\texttt{ pull request \#66}} for details).
\end{DoxyItemize}
\item For {\ttfamily header-\/test}, run {\ttfamily server.\+js} inside that directory and visit \href{http://127.0.0.1:1337/}{\texttt{ http\+://127.\+0.\+0.\+1\+:1337/}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_source_map_support_README_autotoc_md28566}{}\doxysection{License}\label{md_sfcode_node_modules_source_map_support_README_autotoc_md28566}
This code is available under the \href{http://opensource.org/licenses/MIT}{\texttt{ MIT license}}. 