My TypeScript project creates sourcemaps with tsc
. I use the --enable-source-maps
flag with node
, but stacktraces still reference the outputted js
files, not the ts
files, like:
/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
How can I make node
use the source maps to show stacktraces from the ts
files with my npm run dev
command?
Here is my 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"
}
Here is my 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
}
}
You should define node options before executable file, otherwise it would be passed as argument to your build/server/index.js
{
"scripts": {
...
"start": "node --enable-source-maps build/server/index.js"
},
}
You can also use NODE_OPTIONS
env variable to pass node options
NODE_OPTIONS=--enable-source-maps node build/server/index.js