Node忽略TypeScript源映射



我的TypeScript项目用tsc创建源地图。我使用--enable-source-maps标志与node,但堆栈跟踪仍然引用输出的js文件,而不是ts文件,如:

/home/david/Sync/team-tag-mono/build/server/index.js:40
throw new Error('No reason');
^
Error: No reason
at Object.<anonymous> (/home/david/Sync/team-tag-mono/build/server/index.js:40:7)
at Module._compile (node:internal/modules/cjs/loader:1165:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1219:10)
at Module.load (node:internal/modules/cjs/loader:1043:32)
at Function.Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47

我怎么能使node使用源映射显示堆栈跟踪从ts文件与我的npm run dev命令?

这是我的package.json:

{
"name": "reverse-tag-mono",
"version": "1.0.0",
"description": "",
"default": "index.html",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"build:server": "npx tsc",
"build:client": "npx parcel build src/client/index.html",
"build": "npm run build:server && npm run build:client",
"start": "node build/server/index.js --enable-source-maps",
"start:server": "npm run build:server && npm run start",
"dev:server": "npx onchange -i -k "src/shared/**" "src/server/**" -- npm run start:server",
"dev:client": "npx onchange -i -k "src/shared/**" "src/client/**" -- npm run build:client",
"dev": "npx concurrently "npm run dev:server" "npm run dev:client""
},
"repository": {
"type": "git",
"url": "git+https://github.com/danielgstephenson/multisword.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/danielgstephenson/multisword/issues"
},
"homepage": "https://github.com/danielgstephenson/multisword#readme",
"dependencies": {
"csv-append": "^1.0.0",
"express": "^4.18.1",
"matter-js": "^0.18.0",
"socket.io": "^4.5.1",
"socket.io-client": "^4.5.1",
"typescript": "^4.7.4"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/matter-js": "^0.17.7",
"@types/yeast": "^0.1.1",
"buffer": "^6.0.3",
"parcel": "^2.6.2",
"ts-standard": "^11.0.0"
},
"engines": {
"node": "16.14.1"
},
"targets": {
"default": {
"engines": {
"browsers": "> 0.5%, last 2 versions, not dead"
},
"distDir": "dist"
}
},
"browserslist": "> 0.5%, last 2 versions, not dead"
}

这是我的tsconfig.json:

{
"compilerOptions": {
"target": "esnext",                                  
"module": "commonjs",                                
"moduleResolution": "node",
"resolveJsonModule": true,
"sourceMap": true,
"outDir": "./build",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

应该在之前定义节点选项可执行文件,否则它将作为参数传递给build/server/index.js

{
"scripts": {
...
"start": "node --enable-source-maps build/server/index.js"
},
}

您也可以使用NODE_OPTIONSenv变量来传递节点选项

NODE_OPTIONS=--enable-source-maps node build/server/index.js

相关内容

  • 没有找到相关文章

最新更新