微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

节点--enable-source-maps不输出函数名称

如何解决节点--enable-source-maps不输出函数名称

我正在尝试文章Source maps in Node.js中新的实验节点标志--enable-source-maps,尽管它输出正确的行号,但似乎没有输出函数名。

给出以下内容

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()

生成转译的输出和源映射:

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

然后使用node和标志执行它:

❯ 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)

您可以看到行号正确,但是函数名被遮盖了。

如果我重复相同的操作,但使用的是source-map-support

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

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

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

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

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

functionA()

使用node执行它,但不带标志:

❯ 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)

您可以看到正确的行号和函数名称

这是对节点源地图支持的限制吗?还是我误会了?

解决方法

docs中,对于source-map-support似乎有两个选项:

CLI使用情况

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

程序用法

要使用这种方式,您只需将这一行放在文件顶部

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

其中任何一种都可以使用。我相信这个软件包更像是--enable-source-maps标志的替代方案,您无需将它们一起使用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。