'Why does the node inspector not start when I am using nodemon and ts-node?

I have a simple node server written in typescript. My package.json is configured as:

"scripts": {
  "build": "tsc",
  "dev": "nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts",
  "debug": "nodemon  --verbose  --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts"
},

When I run npm run dev nodemon will launch the server and restart it when any changes are made.

[02/28/18 20:45:53]  npm run dev

> [email protected] dev C:\Users\joe\pq\pq-api
> nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts

[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src/**/*
[nodemon] starting `ts-node ./src/server.ts`
initializing config to development
info: PQ-API running on port 3000

However, when I run npm run debug (so I can attach a debugger) It looks like it begins to start, but just hangs forever

[02/28/18 20:39:30]  npm run debug

> [email protected] debug C:\Users\joe\pq\pq-api
> nodemon  --verbose  --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts

[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] or send SIGHUP to 10156 to restart
[nodemon] watching: src/**/*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node --inspect ./src/server.ts`
[nodemon] spawning
[nodemon] child pid: 13344
[nodemon] watching 12 files

That is all the output has. The script is never executed; the server never starts up, and the inspector is never available to connect to.

node 8.94
nodemon 1.15.1
ts-node 5.0.0
typescript 2.7.2



Solution 1:[1]

With ts-node 5.0.0 you no longer pass the --inspect flag the same way. The suggested way is node --inspect -r ts-node/register path/to/ts. For example:

nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register src/app.ts

see https://github.com/TypeStrong/ts-node/issues/537

Solution 2:[2]

Provide location and port to the inspect option like:

--inspect=0.0.0.0:9200

Solution 3:[3]

I just fixed this problem by writing nodemon.json file like this :

{
  "restartable": "rs",
  "ignore": [".git", "node_modules/**/node_modules"],
  "verbose": true,
  "execMap": { // [A]
    "ts": "node --require ts-node/register"
  },
  "watch": ["src/"],
  "env": {
    "NODE_ENV": "development"
  },
  "ext": "js,json,ts"
}

ref : https://dev.to/oieduardorabelo/nodejs-with-typescript-debug-inside-vscode-and-nodemon-23o7

Solution 4:[4]

I made changes using some of the info above as mine just didn't seem to work and the change I made did resolve the problem.

From "start": "tsnd --inspect -- src/app.local.ts"
To: "start": "node --inspect -r ts-node/register src/app.local.ts"

Solution 5:[5]

I had a different issue resulting in the debugger never running. I was running this command,

node index.js --inspect=5005

instead of this,

node --inspect=5005 index.js

The flag is supposed to be before the source file.

Solution 6:[6]

I am using PHP Storm and the previous answer of @user60456 works like a charm for me.

With some changes, I was able to run debug mode using dotenv with multiple env files in PHP Storm as well.

package.json

"start:dev": "nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register -r dotenv/config local.ts dotenv_config_path=./.env.development",

where local.ts is the file where my app.listen() is.

Then, I had to create a new Run configuration (Attach to Node.js/Chrome) with:

  • host: localhost
  • port: 9229
  • Attach to: Chrome or Node.js > 6.3 started with --inspect

Then select the root folder in Remote URLs of local files tab and set the Remote URL to /usr/src/app.

enter image description here

Now, you can run npm run start:dev. You will see the console output:

Debugger listening on ws://127.0.0.1:9229/...

You have to run the Nodej.js debug configuration now and wait, until you see the console output:

Debugger attached.

enter image description here

The application is now running in debug mode.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Faizuddin Mohammed
Solution 3 woodongwoo
Solution 4 Harshit Ruwali
Solution 5 jarora
Solution 6 Tomas Lukac