Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in newest version 5.0.0 debugging is not possible using --inspect parameter #537

Closed
michalchochol opened this issue Feb 21, 2018 · 31 comments

Comments

@michalchochol
Copy link

In ts-node@5.0.0 --inspect parameter does not work anymore. It was working in previous ts-node version.

@egoroof
Copy link

egoroof commented Feb 25, 2018

node --inspect -r ts-node/register path/to/ts

@blakeembrey
Copy link
Member

That’s the intent. Please check the README or change log or more info and use the node command above for inspecting and other node.js flags.

@michalchochol
Copy link
Author

Unfortunately this solution does not work with pm2, which uses ts-node by default to run typescript and passes arguments to ts-node via node_args property.

@blakeembrey
Copy link
Member

Can you provide a reference or code snippet so I can verify that claim? Seems like a simple thing to fix.

@michalchochol
Copy link
Author

michalchochol commented Mar 7, 2018

Hi,

lets assume we have ts-node installed locally via npm and very simple app-test to run:

let fun = () => {console.log('app.js'); setTimeout(fun, 1000);}; 
fun();

Normally to run and debug app using node:

node --inspect=7000 app-test.js //works OK with debugger

node --inspect=7000 -r ts-node/register app-test.ts //works OK with debugger

To run and debug using pm2 (without typescript installed internally in pm2):

pm2 start --node-args="--inspect=7000" app-test.js //works OK with debugger

pm2 start app-test.ts // does not work

pm2 start --node-args="--inspect=7000 -r ts-node/register" app-test.ts // does not work

Last two examples do not work, because by default pm2 uses ts-node interpreter to run typescript -> https://github.com/Unitech/pm2/blob/master/lib/API/interpreter.json

If we install typescript in pm2 by: pm2 install typescript it also installs the latest version of ts-node.

So now we can normally start our script like this:

pm2 start app-test.ts // works OK

pm2 start --node-args="--inspect=7000 -r ts-node/register" app-test.ts // works but without debugger

Last example works without debugger because --node-args are passed to ts-node interpreter and finally ignored. It was working ok with ts-node@4.1.0:

pm2 start --node-args="--inspect=7000" app-test.ts

TEMPORARY WORKAROUND:
After installation of typescript and ts-node modules in pm2, my global pm2 instance keep newest versions of typescript and ts-node in its node_modules subfolder:
~/.nvm/versions/node/v8.9.4/lib/node_modules/pm2/node_modules/ts-node - here is ts-node@5.0.1 located. To enable debugger I had to manually replace this folder with ts-node@4.1.0 folder installed in other place. Now debugger works, because pm2 uses 4.1.0 version by default together with newest typescript@2.7.2.

@blakeembrey
Copy link
Member

Seems like you should submit a fix to pm2. The feature of using node flags via the CLI was an explicit decision and won’t be enabled in future releases.

@blakeembrey
Copy link
Member

Could it be as simple as deleting those interpreters? I noticed that the register method is used somewhere else in the code but I’m not familiar with how it works.

@michalchochol
Copy link
Author

I don't think so it is so simple, but I'm going to try :) Thanks for your time and great project - ts-node really helps and saves a lot of development time.

@marcj
Copy link

marcj commented Mar 14, 2018

Any solution yet to start newest ts-node with inspect via pm2?

@majelbstoat
Copy link

majelbstoat commented Apr 14, 2018

I can't see in the documentation how to combine both a --preserve-symlinks arg for node, and a --project arg for ts-node.

node -r ts-node/register --preserve-symlinks --project tsconfig.server.json says unknown arg --project which makes sense.

ts-node --project tsconfig.server.json --preserve-symlinks doesn't work, probably for the same reason --inspect doesn't work.

--

Update: I was fortunate, and able to use NODE_PRESERVE_SYMLINKS=1 ts-node --project tsconfig.server.json but it is still not clear how to pass args to both ts-node and node at the same time.

@blakeembrey
Copy link
Member

@majelbstoat Unfortunately it's not possible to combine both, that's a limitation with using node.js --require. You need to use environment variables instead.

@Nilos
Copy link

Nilos commented May 3, 2018

For everyone looking into this in the future:

Node supports passing almost any command line options using an environment variable called
NODE_OPTIONS: https://nodejs.org/api/cli.html#cli_node_options_options

@blakeembrey
Copy link
Member

@Nilos Both ways are possible. You can do TS_NODE_PROJECT=xxx node --require ts-node/register --extra-args index.ts or NODE_OPTIONS=xxx ts-node --project=xxx --extra-args index.ts. I documented the first approach in the README, but the second is equally valid, just uses the ts-node executable instead of the node executable. Can you share why you think one is easier than the other since they both require environment variables?

@Nilos
Copy link

Nilos commented May 4, 2018

@blakeembrey I did not know about the TS_NODE_PROJECT option. Is every command line option for ts-node controllable this way?

I actually think TS_NODE_PROJECT is better as my node application spawns other node application which would inherit the NODE_OPTIONS which breaks the sub-processes.

@blakeembrey
Copy link
Member

@agborkowski
Copy link

agborkowski commented Jul 3, 2018

nodemon --config config.json

{ "exec": "node --inspect -r ts-node/register ./src/index.ts" }

works well

full config

{
	"verbose": true,
	"debug": false,
	"exec": "node --inspect -r ts-node/register ./src/index.ts",
	"ignore": [
		"mochawesome-report",
		"node_modules",
		"./test",
		"**/*.d.ts",
		"*.test.ts",
		"*.spec.ts",
		"fixtures/*",
		"test/**/*",
		"docs/*"
	],
	"events": {
		"restart": "echo \"[Warning] Remember run npm run test b4 push to dev branch !\""
	},
	"watch": ["./src"],
	"ext": "ts, gql",
	"inspect": true
}
```

@impmja
Copy link

impmja commented Jul 23, 2018

Thanks @agborkowski , your solution works indeed! 👍

@eddyekofo94
Copy link

@agborkowski Thank you very much brother. God bless

@elliottsj
Copy link

As an alternative to environment variables or ts-node/register, you can pass options in this form:

node [node options] ./node_modules/.bin/ts-node [ts-node options] [script.ts]

e.g.

node --inspect-brk ./node_modules/.bin/ts-node --transpile-only script.ts

@GerbenRampaart
Copy link

@agborkowski Not all heroes wear capes. +1

@yleflour
Copy link

If you want to keep using pm2, the NODE_OPTIONS env variable works well.

Here is my configuration (after running pm2 install typescript)

apps:
  - name: my-app
    script: ./src/index.ts
    watch:
      - src
    env:
      NODE_OPTIONS: --inspect

@hejiaji
Copy link

hejiaji commented Nov 7, 2018

image

Why is that?

@agborkowski @blakeembrey

@blakeembrey
Copy link
Member

@hejiaji You are probably emitting ES6 modules instead of CommonJS? I don't think it has anything to do with ts-node/register vs ts-node CLI. I'd need more information if that's not the cause.

@hejiaji
Copy link

hejiaji commented Nov 7, 2018

Oh right! thanks @blakeembrey , then to more specific on my question:
i have a command like this
ts-node --typeCheck --project tsconfig.server.json --files true server/index.ts
How can i transfer to node command completely?
node -r ts-node/register server/index.ts cannot indicate the --project and --file arguments.

@hejiaji
Copy link

hejiaji commented Nov 7, 2018

Figured it out!! using Environment variable to tackle this!

export TS_NODE_PROJECT=tsconfig.server.json; export TS_NODE_FILES=true && node -r ts-node/register --inspect server/index.ts

Thanks @blakeembrey

@adrian-moisa
Copy link

adrian-moisa commented Feb 27, 2019

node --inspect -r ts-node/register path/to/ts

How can I also provide the --project ./tsconfig-server.json arg for ts-node ? I really need to have a different tsconfig because of react native.

Edit

Found some help here: Overriding tsconfig.json for ts-node in mocha. We need to setup a custom register for ts-node method and call it from the command line.

nodemon.json

{
    "verbose": false,
    "ext": "ts js json yaml",
    "watch": [
        "server/**/*.ts",
        "server/**/*.yaml"
    ],
    "ignore": [],
    "exec": "node --inspect --require ./ts-hook.js ./server/main.ts"
}

ts-hook.js

require("ts-node").register({
    project: "tsconfig-server.json",
});

@marcj
Copy link

marcj commented Feb 27, 2019

@adrian-moisa export TS_NODE_PROJECT=./tsconfig-server.json ts-node path/to/ts.

@michalchochol
Copy link
Author

@yleflour perfect solution :) thanks a lot

If you want to keep using pm2, the NODE_OPTIONS env variable works well.

Here is my configuration (after running pm2 install typescript)

apps:
  - name: my-app
    script: ./src/index.ts
    watch:
      - src
    env:
      NODE_OPTIONS: --inspect

@Evilart86
Copy link

This helps me to configure Webstorm Debuger
node version: 12.3.1
ts-node version: 8.5.4

image

@bhumit070
Copy link

node --inspect -r ts-node/register path/to/ts

in this approach I am using command like this for typescript

nodemon --watch 'src/**/*.ts' --exec node --inspect -r ts-node/register src/index.ts

now I want to pass arguments to ts-node how can I do that can you please help me with that

I want to pass --files arguments to this but in my case I don't think so it is working I am trying like this

nodemon --watch 'src/**/*.ts' --exec node --inspect -r ts-node/register --files src/index.ts

@SishaarRao
Copy link

SishaarRao commented Jan 12, 2023

@bhumit070 If you look at the ts-node documentation for the --files option you can see that there are two ways to enable a setting, either via a flag or via an environment variable.

Environment: TS_NODE_FILES

I would suggest you pass your argument by...

TS_NODE_FILES=true nodemon --watch 'src/**/*.ts' --exec node --inspect -r ts-node/register --files src/index.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests