Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Last active March 14, 2024 11:31
Show Gist options
  • Save cecilemuller/2963155d0f249c1544289b78a1cdd695 to your computer and use it in GitHub Desktop.
Save cecilemuller/2963155d0f249c1544289b78a1cdd695 to your computer and use it in GitHub Desktop.
Run ts-node in VSCode Debugger
{
"version": "0.2.0",
"configurations": [
{
"name": "Example",
"type": "node",
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"args": ["src/script.ts", "--example", "hello"],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**", "node_modules/**"]
}
]
}
@BonesyWonesy
Copy link

👍

@TobiWo
Copy link

TobiWo commented Feb 1, 2022

This is sooooooo helpful. Thanks a ton 👍 ❤️

@wickdninja
Copy link

wickdninja commented Feb 15, 2022

perfect ty! 👍

@pj
Copy link

pj commented Mar 7, 2022

Oh thank god I found this, 2 hours wasted over vscode BS.

@dphov
Copy link

dphov commented Mar 17, 2022

Works perfectly for me

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ts-node",
      "type": "node",
      "request": "launch",
      "args": ["${relativeFile}"],
      "runtimeArgs": ["-r", "ts-node/register"],
      "cwd": "${workspaceRoot}",
      "protocol": "inspector",
      "internalConsoleOptions": "openOnSessionStart"
    }
  ]
}

@jangwd88
Copy link

ManyManyManyManyManyManyManyManyManyManyManyManyManyManyManyManyManyMany
Thank you!!!!!!!!!!

@orbachar
Copy link

orbachar commented Apr 1, 2022

Works perfectly for me

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ts-node",
      "type": "node",
      "request": "launch",
      "args": ["${relativeFile}"],
      "runtimeArgs": ["-r", "ts-node/register"],
      "cwd": "${workspaceRoot}",
      "protocol": "inspector",
      "internalConsoleOptions": "openOnSessionStart"
    }
  ]
}

King!

@kpturner
Copy link

kpturner commented May 4, 2022

Hi everyone! How to deal with typescript paths? It does not recognize imports that are described in tsconfig.json Tried to use NPM package to register paths, but it does not work.

"args": ["${workspaceFolder}/cli/index.ts", "-r", "tsconfig-paths/register"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register", "-r", "tsconfig-paths/register"],

@tusharsnx
Copy link

I am having a strange problem with breakpoints. The debugger never stops at selected breakpoints no matter what. Anyone know how to fix this ?

@scalybur
Copy link

scalybur commented Jul 9, 2022

Any ideas how to get this working with ts-node-dev??

launch.json

{
	// Use IntelliSense to learn about possible attributes.
	// Hover to view descriptions of existing attributes.
	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"name": "Debug ts-node-dev",
			"type": "pwa-node",
			"request": "launch",
			"runtimeExecutable": "node",
			"runtimeArgs": ["--nolazy", "-r", "ts-node-dev/register/transpile-only"],

			"args": ["src/app.ts"],

			"cwd": "${workspaceRoot}",
			"internalConsoleOptions": "openOnSessionStart",
			"skipFiles": ["<node_internals>/**", "node_modules/**"]
		}
	]
}

Error

Process exited with code 1
/usr/local/bin/node --nolazy -r ts-node-dev/register/transpile-only src/app.ts
Uncaught Error: Cannot find module 'ts-node-dev/register/transpile-only'

your launch config

{ "version": "0.2.0", "configurations": [ { "name": "RUN attach to DEV", "type": "pwa-node", "port": 4000, "request": "attach", "trace": true, "skipFiles": [ "<node_internals>/**" ], "restart": true, // "program": "${workspaceFolder}\\index.ts", // "postDebugTask": "npm: clean" }, ...

npm script

"dev": "tsnd --files --transpile-only --respawn --inspect=4000 --project dev.tsconfig.json index.ts node-dev",

@ArnieGA
Copy link

ArnieGA commented Jul 13, 2022

This is great, thx!!
In my case though there's still a pending issue: How to add non-global nodemon into the mix.

@ehaynes99
Copy link

@ArnieGA It took me a while to figure out, so thought I would post it here: https://gist.github.com/ehaynes99/4218666a38984d1c3d544076b49d9439

This uses ts-node to debug, with or without nodemon, without having to install anything.

@ArnieGA
Copy link

ArnieGA commented Aug 1, 2022

@ArnieGA It took me a while to figure out, so thought I would post it here: https://gist.github.com/ehaynes99/4218666a38984d1c3d544076b49d9439

This uses ts-node to debug, with or without nodemon, without having to install anything.

Sorry, this didn't work on my end. My repo contains projects that implement typescript references + paths and those weren't picked up using your method. But that's ok, I went back to the combination that worked for me, which is using a nodemonConfig key plus the start script to achieve what I needed (which does require nodemon to be installed, locally):

node-project/package.json

"nodemonConfig": {
    "exec": "node -r tsconfig-paths/register -r ts-node/register",
    "ext": "ts",
    "ignore": [
        ".git",
        "node_modules"
    ],
    "watch": [
        "./src",
        "../repo-project-1/models",
        "../repo-project-2/src"
    ]
},
"start": "env-cmd -f ./.env.development nodemon src/main.ts"

.vscode/launch.json

{
  "type": "node",
  "request": "launch",
  "name": "Node Project",
  "skipFiles": [
    "<node_internals>/**",
    "node_modules/**"
  ],
  "runtimeExecutable": "npm",
  "runtimeArgs": [
    "start"
  ],
  "outputCapture": "std",
  "cwd": "${workspaceFolder}/node-project",
  "internalConsoleOptions": "openOnSessionStart",
  "stopOnEntry": false,
  "preLaunchTask": "Start Mongo Server",
  "postDebugTask": "Terminate Tasks"
}

This method allowed me to use typescript references + paths, and the ability to pick how to start the project (via debugger or terminal).

@EthanSK
Copy link

EthanSK commented Aug 4, 2022

thanks!!

@manuth
Copy link

manuth commented Aug 16, 2022

I'm getting "unknown file extension" for file "main.ts"

@mrjbj This happens if you're working with an ES Module (a project with a "type": "module" in its package.json-file).
You can find the arguments required for using ESModules in ts-nodes README: https://github.com/TypeStrong/ts-node/tree/8b09d49b26c1c25e34133b857c073729dd691f11#node-flags-and-other-tools

tl;dr-version

For running ESModules using ts-node you might want to use a task-configuration looking like this:

{
    "type": "node",
    "request": "launch",
    "name": "Launch ESModule TypeScript File",
    "cwd": "${workspaceFolder}",
    "runtimeArgs": [
        "--loader",
        "ts-node/esm"
    ],
    "program": "{ Path to Your `.ts` File}"
}

@darius-sas
Copy link

For me it worked after removing /transpile-only from this line

"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],

@resticker
Copy link

resticker commented Sep 2, 2022

This (https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695?permalink_comment_id=4268529#gistcomment-4268529) got me on the right path, but I needed to add "--experimental-specifier-resolution=node", which I realized after remembering i use nodemon like this in my package.json: "npx nodemon --esm --experimentalSpecifierResolution node ./src/index.ts" (note that the camelCase version didn't work, and needed to use the hypenated one experimental-specifier-resolution=node) in order to work with ESM

 "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Server",
      "cwd": "${workspaceFolder}",
      "runtimeArgs": [
        "--loader",
        "ts-node/esm",
        "--experimental-specifier-resolution=node"
      ],
      "program": "${workspaceFolder}/src/index.ts"
    }

Hope this helps someone 🍻

@iofirag
Copy link

iofirag commented Dec 4, 2022

{
"command": "npm start",
"name": "Run npm start",
"request": "launch",
"type": "node-terminal"
}

thanks! :))

@trosck
Copy link

trosck commented Dec 23, 2022

thanks!
my config (I use ts-node bin script):

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "<node_internals>/**",
        "node_modules/**",
      ],
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart",
      "env": {
        "LOCAL_MODE": "true",
      },
      "args": [
        "node_modules/ts-node/dist/bin.js",
        "-r",
        "tsconfig-paths/register",
        "src/index.ts",
      ],
      "runtimeArgs": [
        "--unhandled-rejections=strict",
        "--nolazy",
      ],
    }
  ]
}

@DLatheron
Copy link

I have literally wasted two evenings trying to get my typescript running under VSCode (even though it runs fine using ts-node) - I was just about to throw in the towel and spend the rest of the evening screaming at the moon...

Thank you so much for sharing ❤️

@noirlyrik
Copy link

thanks a lot!

@magickeyyy
Copy link

@rutexd
Copy link

rutexd commented Mar 1, 2023

It actually worked without any problems from first try. Awesome!! 👍

@d0zingcat
Copy link

node-ter

You saved my life! thx!

@kkkkkkhx
Copy link

kkkkkkhx commented Mar 8, 2023

ok

@mfeeney
Copy link

mfeeney commented Apr 14, 2023

I've had the following config working forever without issue, but after upgrading to typescript 5 and apollo server 4, all of the sudden it doesn't like it anymore...

{ "version": "0.2.0", "configurations": [ { "name": "Server", "type": "node", "request": "launch", "runtimeExecutable": "node", "runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"], "resolveSourceMapLocations": ["${workspaceFolder}/**", "!**/node_modules/**"], "args": ["src/index.ts"], "env": { "NODE_ENV": "local" }, "cwd": "${workspaceRoot}", "internalConsoleOptions": "openOnSessionStart", "skipFiles": ["<node_internals>/**", "node_modules/**"] } ] }

I end up getting this, which looks like it's not skipping the node_modules like it should
/node_modules/@apollo/server/dist/cjs/ApolloServer.js:70 const nodeEnv = config.nodeEnv ?? process.env.NODE_ENV ?? '';

Anyone else seen anything like this?

@mcustiel
Copy link

Thank you very much. I used your config to be able to debug typescript in neovim using dap.

@L9m
Copy link

L9m commented Jun 11, 2023

no type checking and need to use tsconfig-paths to resolve paths

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "runtimeArgs": ["-r", "ts-node/register/transpile-only", "-r", "tsconfig-paths/register"],
            "program": "${workspaceFolder}/packages/lerna/src/cli.js",
            "args": ["version"],
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}",
            "internalConsoleOptions": "neverOpen",
        }
    ]
}

@pablezhang
Copy link

Error: Cannot find module 'ts-node/register/transpile-only'

fixed this by install ts-node locally

npm link ts-node

// or 

yarn add -D ts-node

@iulo thanks,u save my day.

@silvioprog
Copy link

Works perfectly for me

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ts-node",
      "type": "node",
      "request": "launch",
      "args": ["${relativeFile}"],
      "runtimeArgs": ["-r", "ts-node/register"],
      "cwd": "${workspaceRoot}",
      "protocol": "inspector",
      "internalConsoleOptions": "openOnSessionStart"
    }
  ]
}

Not all heroes wear capes! 😀

@Davetwo
Copy link

Davetwo commented Mar 8, 2024

Thanks!
General purpose ts-node run opened file (using ${file}) whereas process-library is the base-folder of the ts app:

  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  // https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ts-node",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "node",
      "runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],

      "args": ["${file}"],
      
      "cwd": "${workspaceRoot}/process-library",
      "internalConsoleOptions": "openOnSessionStart",
      "skipFiles": ["<node_internals>/**", "node_modules/**"]
    }
    
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment