node --enable-source-maps not outputting function names

Viewed 1823

I am trying the new experimental node flag --enable-source-maps from the article Source maps in Node.js and while it outputs the correct line numbers, it doesn't appear to be outputting function names.

Given the following:

package.json

{
  "name": "node-sourcemaps",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "source-map-support": "^0.5.19",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  }
}

index.js

const functionA = () => {
  functionB()
}

const functionB = () => {
  functionC()
}

const functionC = () => {
  functionD()
}

const functionD = () => {
  throw new Error('Roh Ruh')
}

functionA()

Generating the transpiled output and source map:

> npx webpack ./index.js --devtool source-map --target node --mode production

And then executing it using node and the flag:

❯ node --enable-source-maps .\dist\main.js
Error: Roh Ruh
    at o (D:\spikes\node-sourcemaps-spike\dist\main.js:1:970)
        -> D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:14:9
    at n (D:\spikes\node-sourcemaps-spike\dist\main.js:1:952)
        -> D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:10:3
    at r (D:\spikes\node-sourcemaps-spike\dist\main.js:1:940)
        -> D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:6:3
    at Object.<anonymous> (D:\spikes\node-sourcemaps-spike\dist\main.js:1:992)   
        -> D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:2:3
    at r (D:\spikes\node-sourcemaps-spike\dist\main.js:1:110)
        -> D:\spikes\node-sourcemaps-spike\dist\webpack:\webpack\bootstrap:19:22 
    at D:\spikes\node-sourcemaps-spike\dist\main.js:1:902
        -> D:\spikes\node-sourcemaps-spike\dist\webpack:\webpack\bootstrap:83:10 
    at Object.<anonymous> (D:\spikes\node-sourcemaps-spike\dist\main.js:1:911)   
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)

    at Module.load (internal/modules/cjs/loader.js:812:32)

You can see that the line numbers are correct, however the function names are obscured.

If I repeat the same but using the source-map-support package

require('source-map-support').install()

const functionA = () => {
  functionB()
}

const functionB = () => {
  functionC()
}

const functionC = () => {
  functionD()
}

const functionD = () => {
  throw new Error('Roh Ruh')
}

functionA()

Executing it using node but without the flag:

❯ node .\dist\main.js

D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:16
  throw new Error('Roh Ruh')
        ^
Error: Roh Ruh
    at functionD (D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:16:9)   
    at functionC (D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:12:3)   
    at functionB (D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:8:3)    
    at Object.call (D:\spikes\node-sourcemaps-spike\dist\webpack:\index.js:4:3)  
    at __webpack_require__ (D:\spikes\node-sourcemaps-spike\dist\webpack:\webpack\bootstrap:19:22)
    at D:\spikes\node-sourcemaps-spike\dist\webpack:\webpack\bootstrap:83:10     
    at Object.<anonymous> (D:\spikes\node-sourcemaps-spike\dist\main.js:1:911)   
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)     
    at Module.load (internal/modules/cjs/loader.js:812:32)

You can see the correct line numbers and function names.

Is this a limitation of node sourcemap support or am I misunderstanding?

1 Answers

It looks like in the docs for source-map-support, you have 2 options:

CLI Usage

// edited to match your example
node -r source-map-support/register dist/main.js

Programmatic Usage

To use this way, you simply put this line at the top of your file

require('source-map-support').install();

Either one of these will work. I believe this package is more of an alternative to the --enable-source-maps flag and you don't need to use them together

Related