You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
9.2 KiB

4 years ago
  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, place a source mapping comment somewhere in the file (usually done automatically or with an option by your transpiler):
  10. ```
  11. //# sourceMappingURL=path/to/source.map
  12. ```
  13. If multiple sourceMappingURL comments exist in one file, the last sourceMappingURL comment will be
  14. respected (e.g. if a file mentions the comment in code, or went through multiple transpilers).
  15. The path should either be absolute or relative to the compiled file.
  16. From here you have two options.
  17. ##### CLI Usage
  18. ```bash
  19. node -r source-map-support/register compiled.js
  20. ```
  21. ##### Programmatic Usage
  22. Put the following line at the top of the compiled file.
  23. ```js
  24. require('source-map-support').install();
  25. ```
  26. It is also possible to install the source map support directly by
  27. requiring the `register` module which can be handy with ES6:
  28. ```js
  29. import 'source-map-support/register'
  30. // Instead of:
  31. import sourceMapSupport from 'source-map-support'
  32. sourceMapSupport.install()
  33. ```
  34. Note: if you're using babel-register, it includes source-map-support already.
  35. It is also very useful with Mocha:
  36. ```
  37. $ mocha --require source-map-support/register tests/
  38. ```
  39. #### Browser support
  40. 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.
  41. 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.
  42. ```html
  43. <script src="browser-source-map-support.js"></script>
  44. <script>sourceMapSupport.install();</script>
  45. ```
  46. 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:
  47. ```html
  48. <script>
  49. define(['browser-source-map-support'], function(sourceMapSupport) {
  50. sourceMapSupport.install();
  51. });
  52. </script>
  53. ```
  54. ## Options
  55. 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:
  56. ```js
  57. require('source-map-support').install({
  58. handleUncaughtExceptions: false
  59. });
  60. ```
  61. 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.
  62. ```js
  63. require('source-map-support').install({
  64. retrieveSourceMap: function(source) {
  65. if (source === 'compiled.js') {
  66. return {
  67. url: 'original.js',
  68. map: fs.readFileSync('compiled.js.map', 'utf8')
  69. };
  70. }
  71. return null;
  72. }
  73. });
  74. ```
  75. 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.
  76. 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'.
  77. ```js
  78. require('source-map-support').install({
  79. environment: 'node'
  80. });
  81. ```
  82. To support files with inline source maps, the `hookRequire` options can be specified, which will monitor all source files for inline source maps.
  83. ```js
  84. require('source-map-support').install({
  85. hookRequire: true
  86. });
  87. ```
  88. This monkey patches the `require` module loading chain, so is not enabled by default and is not recommended for any sort of production usage.
  89. ## Demos
  90. #### Basic Demo
  91. original.js:
  92. ```js
  93. throw new Error('test'); // This is the original code
  94. ```
  95. compiled.js:
  96. ```js
  97. require('source-map-support').install();
  98. throw new Error('test'); // This is the compiled code
  99. // The next line defines the sourceMapping.
  100. //# sourceMappingURL=compiled.js.map
  101. ```
  102. compiled.js.map:
  103. ```json
  104. {
  105. "version": 3,
  106. "file": "compiled.js",
  107. "sources": ["original.js"],
  108. "names": [],
  109. "mappings": ";;AAAA,MAAM,IAAI"
  110. }
  111. ```
  112. Run compiled.js using node (notice how the stack trace uses original.js instead of compiled.js):
  113. ```
  114. $ node compiled.js
  115. original.js:1
  116. throw new Error('test'); // This is the original code
  117. ^
  118. Error: test
  119. at Object.<anonymous> (original.js:1:7)
  120. at Module._compile (module.js:456:26)
  121. at Object.Module._extensions..js (module.js:474:10)
  122. at Module.load (module.js:356:32)
  123. at Function.Module._load (module.js:312:12)
  124. at Function.Module.runMain (module.js:497:10)
  125. at startup (node.js:119:16)
  126. at node.js:901:3
  127. ```
  128. #### TypeScript Demo
  129. demo.ts:
  130. ```typescript
  131. declare function require(name: string);
  132. require('source-map-support').install();
  133. class Foo {
  134. constructor() { this.bar(); }
  135. bar() { throw new Error('this is a demo'); }
  136. }
  137. new Foo();
  138. ```
  139. Compile and run the file using the TypeScript compiler from the terminal:
  140. ```
  141. $ npm install source-map-support typescript
  142. $ node_modules/typescript/bin/tsc -sourcemap demo.ts
  143. $ node demo.js
  144. demo.ts:5
  145. bar() { throw new Error('this is a demo'); }
  146. ^
  147. Error: this is a demo
  148. at Foo.bar (demo.ts:5:17)
  149. at new Foo (demo.ts:4:24)
  150. at Object.<anonymous> (demo.ts:7:1)
  151. at Module._compile (module.js:456:26)
  152. at Object.Module._extensions..js (module.js:474:10)
  153. at Module.load (module.js:356:32)
  154. at Function.Module._load (module.js:312:12)
  155. at Function.Module.runMain (module.js:497:10)
  156. at startup (node.js:119:16)
  157. at node.js:901:3
  158. ```
  159. There is also the option to use `-r source-map-support/register` with typescript, without the need add the `require('source-map-support').install()` in the code base:
  160. ```
  161. $ npm install source-map-support typescript
  162. $ node_modules/typescript/bin/tsc -sourcemap demo.ts
  163. $ node -r source-map-support/register demo.js
  164. demo.ts:5
  165. bar() { throw new Error('this is a demo'); }
  166. ^
  167. Error: this is a demo
  168. at Foo.bar (demo.ts:5:17)
  169. at new Foo (demo.ts:4:24)
  170. at Object.<anonymous> (demo.ts:7:1)
  171. at Module._compile (module.js:456:26)
  172. at Object.Module._extensions..js (module.js:474:10)
  173. at Module.load (module.js:356:32)
  174. at Function.Module._load (module.js:312:12)
  175. at Function.Module.runMain (module.js:497:10)
  176. at startup (node.js:119:16)
  177. at node.js:901:3
  178. ```
  179. #### CoffeeScript Demo
  180. demo.coffee:
  181. ```coffee
  182. require('source-map-support').install()
  183. foo = ->
  184. bar = -> throw new Error 'this is a demo'
  185. bar()
  186. foo()
  187. ```
  188. Compile and run the file using the CoffeeScript compiler from the terminal:
  189. ```sh
  190. $ npm install source-map-support coffeescript
  191. $ node_modules/.bin/coffee --map --compile demo.coffee
  192. $ node demo.js
  193. demo.coffee:3
  194. bar = -> throw new Error 'this is a demo'
  195. ^
  196. Error: this is a demo
  197. at bar (demo.coffee:3:22)
  198. at foo (demo.coffee:4:3)
  199. at Object.<anonymous> (demo.coffee:5:1)
  200. at Object.<anonymous> (demo.coffee:1:1)
  201. at Module._compile (module.js:456:26)
  202. at Object.Module._extensions..js (module.js:474:10)
  203. at Module.load (module.js:356:32)
  204. at Function.Module._load (module.js:312:12)
  205. at Function.Module.runMain (module.js:497:10)
  206. at startup (node.js:119:16)
  207. ```
  208. ## Tests
  209. 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:
  210. * Build the tests using `build.js`
  211. * Launch the HTTP server (`npm run serve-tests`) and visit
  212. * http://127.0.0.1:1336/amd-test
  213. * http://127.0.0.1:1336/browser-test
  214. * 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).
  215. * For `header-test`, run `server.js` inside that directory and visit http://127.0.0.1:1337/
  216. ## License
  217. This code is available under the [MIT license](http://opensource.org/licenses/MIT).