ts-node-dev: TypeScript Slow Restarting Time

When I start app ts compile very fast, but when I change some file It take to long to restart, in which may be problem ?

./node_modules/.bin/tsc -project ./tsconfig.json --diagnostics

Files:           403
Lines:         64126
Nodes:        237079
Identifiers:   83972
Symbols:       67964
Types:          7966
Memory used: 112027K
I/O read:      0.04s
I/O write:     0.09s
Parse time:    0.89s
Bind time:     0.28s
Check time:    0.66s
Emit time:     0.45s
Total time:    2.28s

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["es6", "es2017"],
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",

    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": ["node_modules"],
  "include": ["src"]
}

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 27
  • Comments: 38 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I have the same problem and just did some debugging as @whitecolor advised.

This is what I found

This line: https://github.com/whitecolor/ts-node-dev/blob/master/lib/index.js#L55 (watcher.removeAll()) takes 31 seconds to execute. Upon further investigation, it turns out that watcher.list() contains a whopping 1323 elements, which includes mainly the files in node_modules.

Solution

Include the --ignore-watch node_modules flag in your dev script. Mine now looks like this:

ts-node-dev --respawn --transpileOnly --ignore-watch node_modules ./app/app.ts

watcher.list() now only contains my 27 source files, and restarting my app is done in about 1 second again.

My solution with nodemon:

Install nodemon

npm i nodemon -D

Create nodemon.json config

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "npm start"
}

Start script

"start": "ts-node -r tsconfig-paths/register src/index.ts",

I have the same problem and the solution that worked for me is to use--exit-child Another solution is graceful shutdown The problem is that ts-node cannot kill a child process for some reason.

I ended up here ts-node-dev --poll --respawn --transpile-only --no-deps --exit-child ./src/index.ts

thanks to https://github.com/wclr/ts-node-dev/issues/69#issuecomment-493675960

I’ve been facing this issue as well. A normal stop + start of the server using ts-node takes a second, while when I use ts-node-dev, it detects a file change instantly, but post that it takes about 5 seconds to issue a server restart.

I’m using ts-node-dev like so -

ts-node-dev --no-notify --respawn --transpileOnly src/index.ts

I got same problem and --ignore-watch node_modules resolved it too.

And I found --no-deps also works as node-dev README says

By default node-dev will watch all first-level dependencies, i.e. the ones in the project’s node_modulesfolder.

–no-deps Watch only the project’s own files and linked modules (via npm link)

a little simpler …?

--no-deps or --ignore-watch node_modules should definitely be enabled by default

so start up is very quick, but when restarting after saving a file i get…

[INFO] 23:53:15 Restarting: /Users/<me>/<long-path-to-project-and-file>.ts has been modified

it stays on this for about 45 seconds, as you can see from the time.

Then this runs.

[DEBUG] 23:54:03 Child is still running, restart upon exit
[DEBUG] 23:54:03 Disconnecting from child
[DEBUG] 23:54:03 Sending SIGTERM kill to child
[DEBUG] 23:54:03 Child exited with code 0
Using ts-node version 8.0.3, typescript version 3.4.3
[DEBUG] 23:54:03 Starting child process -r /var/folders/9r/fjpzdr5d6p76vtl2gldf3zh40000gn/T/ts-node-dev-hook-19235789589911567.js /Users/<user>/<path-to>/node_modules/ts-node-dev/lib/wrap.js src/index.ts

Passing the --poll argument to ts-node-dev fixed the hang on Removing all watchers from files for me without ignoring anything (on OSX).

// tsconfig.json

{
  "ts-node": {
     "transpileOnly": true
  },
  "compilerOptions": {...}
}

--transpileOnly is deprecated. Use --transpile-only instead

I had the same problem where Removing all watchers from files hung for a very long time on restarts, I gave up waiting. (If relevant: on MacOS and Activity Monitor showed fseventsd using a lot of CPU.)

Solution

Include the --ignore-watch node_modules flag in your dev script.

Adding this argument --ignore-watch node_modules was the solution for me also.

From reading the README.md I thought this was the default, but maybe I misunderstood?

image

@intellix I’m not sure this would help you, but take a look at this lib: https://www.npmjs.com/package/patch-package

I’m thinking you could use --ignore-watch node_modules and still reload the server by using this lib. Besides, it’s a nice lib if you didn’t know it yet 😃

doesn’t restart at all for me, I think it’s related change is detected, because I see [INFO] 23:39:05 Restarting: /Users/shook/Development/sHooKDT-task-2018/src/index.ts has been modified in terminal, but nothing happens even after 15 minutes