Reputation: 49570
How to attach the VSCode debugger to the ts-node
command that uses env variables like the below one:
package.json:
{
"runMyScript": "ts-node -r dotenv/config --transpile-only tasks/migration/myScript.ts dotenv_config_path=./local.env"
}
I tried adding flags --respawn --inspect=4000
to the above command with below launch.json, but it didn't work:
launch.json:
{"configurations": [
{
"name": "RUN attach to DEV",
"type": "node",
"port": 4000,
"request": "attach",
"trace": true,
"skipFiles": ["<node_internals>/**"],
"restart": true
},
]}
Upvotes: 14
Views: 23168
Reputation: 24628
MODULE_NOT_FOUND
and "Cannot find module @myproj/MY_PACKAGE"
paths
supportpaths
config option, which is commonly used for allowing cross-referencing packages in monorepos. Those will result in MODULE_NOT_FOUND
errors.TypeScript
contributors stated that they don't intend to resolve paths
when building.ts-node
.tsconfig-paths
and ts-node
(or tsx
) as dev dependencies.cd packages/MY_PACKAGE && npx ts-node -r tsconfig-paths/register src/index.ts
{
// ...
"configurations": [
// ...
{
"name": "ts-node MY_PACKAGE",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/ts-node",
// "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/tsx",
"runtimeArgs": ["-r", "tsconfig-paths/register"],
"cwd": "${workspaceFolder}/packages/MY_PACKAGE",
"program": "src/index.ts"
}
]
}
tsconfig-paths
must reference a tsconfig.json
directly. You must either:
cd
into (set cwd
to) the path that has the target tsconfig.json
,TS_NODE_PROJECT
.tsx
and ts-node
. tsx
is a lot faster but does not do any type checking.outFiles
, since this solution uses injection-based transpilation, i.e. it has the compiled *.js
files in memory, and does not write them to disk.Upvotes: 1
Reputation: 503
{
"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/**"]
}
]
}
This works for me.
Upvotes: 1
Reputation: 2404
From the official documentation: https://typestrong.org/ts-node/docs/recipes/visual-studio-code
{
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch Program",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts"
]
}],
}
Upvotes: 7
Reputation: 15683
Here is a .vscode/launch.json
config that allows you to...
npm
script specified in package.json
nvm
is used)Given the following scripts
section in your package.json
:
"scripts": {
"start": "node -r ts-node/register --env-file=.myenvfile src/index.ts",
},
You can attach your vscode debugger with the the following launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "start",
"type": "node",
"request": "launch",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "start"],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
"runtimeVersion": "21"
}
]
}
See https://code.visualstudio.com/docs/editor/debugging for further reference.
Upvotes: 3
Reputation: 6127
.vscode/launch.json
or add configurations to your existing configurations{
"version": "1.0.0",
"configurations": [
{
"name": "TS-Node",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/ts-node",
"runtimeArgs": [
"--transpile-only",
// if you use esm
"--esm"
],
"program": "${file}",
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**", "node_modules/**"]
}
]
}
Run and Debug
window on the left side of VSCode and run this configuration called TS-Node
Upvotes: 12
Reputation: 1375
This solution works for me https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695
The idea is to use: node -r ts-node/register/transpile-only
{
...
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
}
Upvotes: 5