62bd58ff21365ead44babdd346907abc841d664e359c24a71e2056cb71148c9a87e8c2f69dc0fb29088b9022a86f86f6f9f361b6b74794af1dcf80b397e5db 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Source Map Support
  2. [![Build Status](https://travis-ci.org/evanw/node-source-map-support.svg?branch=master)](https://travis-ci.org/evanw/node-source-map-support)
  3. This module provides source map support for stack traces in node via the [V8 stack trace API](https://github.com/v8/v8/wiki/Stack-Trace-API). It uses the [source-map](https://github.com/mozilla/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.
  4. ## Installation and Usage
  5. #### Node support
  6. ```
  7. $ npm install source-map-support
  8. ```
  9. Source maps can be generated using libraries such as [source-map-index-generator](https://github.com/twolfson/source-map-index-generator). Once you have a valid source map, insert the following line at the top of your compiled code:
  10. ```js
  11. require('source-map-support').install();
  12. ```
  13. And place a source mapping comment somewhere in the file (usually done automatically or with an option by your transpiler):
  14. ```
  15. //# sourceMappingURL=path/to/source.map
  16. ```
  17. If multiple sourceMappingURL comments exist in one file, the last sourceMappingURL comment will be
  18. respected (e.g. if a file mentions the comment in code, or went through multiple transpilers).
  19. The path should either be absolute or relative to the compiled file.
  20. It is also possible to to install the source map support directly by
  21. requiring the `register` module which can be handy with ES6:
  22. ```js
  23. import 'source-map-support/register'
  24. // Instead of:
  25. import sourceMapSupport from 'source-map-support'
  26. sourceMapSupport.install()
  27. ```
  28. Note: if you're using babel-register, it includes source-map-support already.
  29. It is also very useful with Mocha:
  30. ```
  31. $ mocha --require source-map-support/register tests/
  32. ```
  33. #### Browser support
  34. This library also works in Chrome. While the DevTools console already supports source maps, the V8 engine doesn't and `Error.prototype.stack` will be incorrect without this library. Everything will just work if you deploy your source files using [browserify](http://browserify.org/). Just make sure to pass the `--debug` flag to the browserify command so your source maps are included in the bundled code.
  35. This library also works if you use another build process or just include the source files directly. In this case, include the file `browser-source-map-support.js` in your page and call `sourceMapSupport.install()`. It contains the whole library already bundled for the browser using browserify.
  36. ```html
  37. <script src="browser-source-map-support.js"></script>
  38. <script>sourceMapSupport.install();</script>
  39. ```
  40. This library also works if you use AMD (Asynchronous Module Definition), which is used in tools like [RequireJS](http://requirejs.org/). Just list `browser-source-map-support` as a dependency:
  41. ```html
  42. <script>
  43. define(['browser-source-map-support'], function(sourceMapSupport) {
  44. sourceMapSupport.install();
  45. });
  46. </script>
  47. ```
  48. ## Options
  49. 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 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:
  50. ```js
  51. require('source-map-support').install({
  52. handleUncaughtExceptions: false
  53. });
  54. ```
  55. This module loads source maps from the filesystem by default. You can provide alternate loading behavior through a callback as shown below. For example, [Meteor](https://github.com/meteor) keeps all source maps cached in memory to avoid disk access.
  56. ```js
  57. require('source-map-support').install({
  58. retrieveSourceMap: function(source) {
  59. if (source === 'compiled.js') {
  60. return {
  61. url: 'original.js',
  62. map: fs.readFileSync('compiled.js.map', 'utf8')
  63. };
  64. }
  65. return null;
  66. }
  67. });
  68. ```
  69. The module will by default assume a browser environment if XMLHttpRequest and window are defined. If either of these do not exist it will instead assume a node environment.
  70. 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 'browser' or 'node'.
  71. ```js
  72. require('source-map-support').install({
  73. environment: 'node'
  74. });
  75. ```
  76. To support files with inline source maps, the `hookRequire` options can be specified, which will monitor all source files for inline source maps.
  77. ```js
  78. require('source-map-support').install({
  79. hookRequire: true
  80. });
  81. ```
  82. This monkey patches the `require` module loading chain, so is not enabled by default and is not recommended for any sort of production usage.
  83. ## Demos
  84. #### Basic Demo
  85. original.js:
  86. ```js
  87. throw new Error('test'); // This is the original code
  88. ```
  89. compiled.js:
  90. ```js
  91. require('source-map-support').install();
  92. throw new Error('test'); // This is the compiled code
  93. // The next line defines the sourceMapping.
  94. //# sourceMappingURL=compiled.js.map
  95. ```
  96. compiled.js.map:
  97. ```json
  98. {
  99. "version": 3,
  100. "file": "compiled.js",
  101. "sources": ["original.js"],
  102. "names": [],
  103. "mappings": ";;AAAA,MAAM,IAAI"
  104. }
  105. ```
  106. Run compiled.js using node (notice how the stack trace uses original.js instead of compiled.js):
  107. ```
  108. $ node compiled.js
  109. original.js:1
  110. throw new Error('test'); // This is the original code
  111. ^
  112. Error: test
  113. at Object.<anonymous> (original.js:1:7)
  114. at Module._compile (module.js:456:26)
  115. at Object.Module._extensions..js (module.js:474:10)
  116. at Module.load (module.js:356:32)
  117. at Function.Module._load (module.js:312:12)
  118. at Function.Module.runMain (module.js:497:10)
  119. at startup (node.js:119:16)
  120. at node.js:901:3
  121. ```
  122. #### TypeScript Demo
  123. demo.ts:
  124. ```typescript
  125. declare function require(name: string);
  126. require('source-map-support').install();
  127. class Foo {
  128. constructor() { this.bar(); }
  129. bar() { throw new Error('this is a demo'); }
  130. }
  131. new Foo();
  132. ```
  133. Compile and run the file using the TypeScript compiler from the terminal:
  134. ```
  135. $ npm install source-map-support typescript
  136. $ node_modules/typescript/bin/tsc -sourcemap demo.ts
  137. $ node demo.js
  138. demo.ts:5
  139. bar() { throw new Error('this is a demo'); }
  140. ^
  141. Error: this is a demo
  142. at Foo.bar (demo.ts:5:17)
  143. at new Foo (demo.ts:4:24)
  144. at Object.<anonymous> (demo.ts:7:1)
  145. at Module._compile (module.js:456:26)
  146. at Object.Module._extensions..js (module.js:474:10)
  147. at Module.load (module.js:356:32)
  148. at Function.Module._load (module.js:312:12)
  149. at Function.Module.runMain (module.js:497:10)
  150. at startup (node.js:119:16)
  151. at node.js:901:3
  152. ```
  153. #### CoffeeScript Demo
  154. demo.coffee:
  155. ```coffee
  156. require('source-map-support').install()
  157. foo = ->
  158. bar = -> throw new Error 'this is a demo'
  159. bar()
  160. foo()
  161. ```
  162. Compile and run the file using the CoffeeScript compiler from the terminal:
  163. ```sh
  164. $ npm install source-map-support coffee-script
  165. $ node_modules/coffee-script/bin/coffee --map --compile demo.coffee
  166. $ node demo.js
  167. demo.coffee:3
  168. bar = -> throw new Error 'this is a demo'
  169. ^
  170. Error: this is a demo
  171. at bar (demo.coffee:3:22)
  172. at foo (demo.coffee:4:3)
  173. at Object.<anonymous> (demo.coffee:5:1)
  174. at Object.<anonymous> (demo.coffee:1:1)
  175. at Module._compile (module.js:456:26)
  176. at Object.Module._extensions..js (module.js:474:10)
  177. at Module.load (module.js:356:32)
  178. at Function.Module._load (module.js:312:12)
  179. at Function.Module.runMain (module.js:497:10)
  180. at startup (node.js:119:16)
  181. ```
  182. ## Tests
  183. This repo contains both automated tests for node and manual tests for the browser. The automated tests can be run using mocha (type `mocha` in the root directory). To run the manual tests:
  184. * Build the tests using `build.js`
  185. * Launch the HTTP server (`npm run serve-tests`) and visit
  186. * http://127.0.0.1:1336/amd-test
  187. * http://127.0.0.1:1336/browser-test
  188. * http://127.0.0.1:1336/browserify-test - **Currently not working** due to a bug with browserify (see [pull request #66](https://github.com/evanw/node-source-map-support/pull/66) for details).
  189. * For `header-test`, run `server.js` inside that directory and visit http://127.0.0.1:1337/
  190. ## License
  191. This code is available under the [MIT license](http://opensource.org/licenses/MIT).