Node忽略TypeScript源Map

s1ag04yj  于 2023-04-13  发布在  TypeScript
关注(0)|答案(1)|浏览(128)

我的TypeScript项目使用tsc创建sourcemap。我使用--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

如何使用npm run dev命令使node使用源Map来显示ts文件中的堆栈跟踪?
这是我的包。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
  }
}
mnemlml8

mnemlml81#

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

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

也可以使用NODE_OPTIONS env变量传递节点选项

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

相关问题