3

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?

2
  • what version is your node? it looks like support for the --enable-source-maps flag was added in v12.12.0 Commented Sep 24, 2020 at 0:43
  • I using node v12.13.0 Commented Sep 24, 2020 at 3:25

1 Answer 1

-2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I don't understand. If I use the source-map-support package I get the results I'm after. I'm trying to reproduce the same results without using the source-map-support by using nodes built in --enable-source-maps flag, however in this case I get the line numbers but not the function names
sorry, it seems i misunderstood the question. it seems like the --enable-source-maps flag is working as intended, i don't think it is intended to display the function names, just to point to the original file. it seems like adding the function line and names is a feature of the source-map-support package @kimsagro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.