Skip to content

Instantly share code, notes, and snippets.

@brokenthorn
Last active September 14, 2020 14:54
Show Gist options
  • Save brokenthorn/b11366610a05330fe3b5fef6939d0ca7 to your computer and use it in GitHub Desktop.
Save brokenthorn/b11366610a05330fe3b5fef6939d0ca7 to your computer and use it in GitHub Desktop.
Restartable debugging for TypeScript within Visual Studio Code (vscode) using nodemon and ts-node

Info

Inspiration was taken from Eduardo Rabelo - https://eduardorabelo.me - from this article: https://dev.to/oieduardorabelo/nodejs-with-typescript-debug-inside-vscode-and-nodemon-23o7

To start debugging, you need to first start the dev:debug npm script with npm run dev:debug, then run the Node: Nodemon launch configuration from VSCode, in order to attach the debugger to the app process.

Pick the nodemon process when prompted, the one that should look like npx nodemon --inspect src/index.ts. It's the process that stays alive and restarts the app process when you make changes (of course it first feeds the *.ts code to ts-node for transpilation because of that node --require ts-node/register line in nodemon.json).

Template

Eduardo also has a nice template for bootstrapping this setup for new Node+TypeScript projects: https://github.com/oieduardorabelo/node-typescript

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Node: Nodemon",
"processId": "${command:PickProcess}",
"restart": true,
"protocol": "inspector"
}
]
}
{
"restartable": "rs",
"ignore": [".git", "node_modules/**/node_modules"],
"verbose": true,
"execMap": {
"ts": "node --require ts-node/register"
},
"watch": ["src/"],
"env": {
"NODE_ENV": "development"
},
"ext": "js,json,ts"
}
{
"scripts": {
"dev": "npx nodemon src/index.ts",
"dev:debug": "npx nodemon --inspect src/index.ts"
}
}
{
"version": "4.0.2",
"compilerOptions": {
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
"target": "es2019",
"lib": ["es2019"],
"module": "commonjs",
"moduleResolution": "node",
"alwaysStrict": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"pretty": true,
"skipLibCheck": true,
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": ".",
"outDir": "./build",
"rootDir": "./src",
"typeRoots": ["./node_modules/@types", "./node_modules"],
"types": ["node"]
},
"include": ["src/**/*"],
"exclude": ["./node_modules", "**/*.test.ts"],
"compileOnSave": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment