2

有由 babel 转译的代码。但在运行时错误时,它显示错误的行号。

我以这种方式运行脚本。

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

它使用跑步者。

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

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

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

Babel register 使用来自 babel.config.js 的配置

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

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

当我在运行时的代码中抛出错误时。它显示了错误的行号。我了解源地图支持不起作用。

VSCode 调试顺利。编辑器查看并理解源映射。

帮助我使源地图可行。

4

1 回答 1

0

babel.config.js 中babel-plugin-source-map-support的插件注册丢失。阅读库babel-plugin-source-map-support的描述 需要两个库:babel-plugin-source-map-supportsource-map-support。安装它们。

在 babel 文件中,注册 source-map-support 插件

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

在顶部的文件中启用运行时。

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

现在,当它失败时,它必须显示源代码的正确错误行号。

于 2021-12-16T11:17:16.363 回答