@babel/register support source maps on runtime

861 Views Asked by At

There is code that is transpiled by babel. But on runtime error it shows a wrong line number.

I run the script this way.

node -r ./runner.js ./index.js

It uses the runner.

console.log('Runner. Registers babel.')

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

require('@babel/register')({
  extensions: ['.js'],
  ignore: [
    /node_modules[\\/](?!console-command-manager)/
  ],
});

Babel register uses the config from babel.config.js

console.log('Babel. Configuration.');

module.exports = {
  presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
  plugins: [],
  sourceMap: "inline"
};

When I throw an error in the code on runtime. It shows me wrong line numbers. I understand that source-map-support does not work.

VSCode debugging goes well. The editor see and understand source maps.

Help me to make source-maps workable.

1

There are 1 best solutions below

0
Victor Shelepen On

The plugin registration of babel-plugin-source-map-support in babel.config.js is missed. Read the description of the library babel-plugin-source-map-support There two libraries are needed: babel-plugin-source-map-support and source-map-support. Install them both.

In babel file, register source-map-support plugin

{
  sourceMaps: true,
  plugins: ['source-map-support', ...]
}

Enable on runtime in a file at the top.

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

Now, when it fails it has to show the right error line number of the source code.