Ts-node: How to run like nodemon when code is changed?

Created on 1 Nov 2016  路  11Comments  路  Source: TypeStrong/ts-node

Wanna automatically recompile&run when code is changed.

like nodemon

maybe ..

ts-nodemon xxx
or
ts-node xxx --watch ?

question

Most helpful comment

For anyone who might come here for a nodemon solution as me.

Add these 2 property in nodemon.json file:

```json
"ext": "ts,yaml,js,json",
"execMap": {
"ts": "ts-node"
},
````

And then you can use nodemon app.ts smoothly.

Be careful for the generated .js file from tsc , they will overlay ts files when ts-node requires modules.

All 11 comments

Why not just use nodemon or onchange, etc? There's directions in their respective READMEs to set the exec commands.

Thanks !
onchange works! (never know this before)

Not just use nodemon because tsc -w (disk) may be slower than ts-node (memory).
Not sure...

FWIW, my personal development setup involves concurrently (the module) running tsc -w and onchange watching the build output directory. I don't actually use ts-node myself except for tests and demos.

So whether ts-node build in memory or disk ?
I thought it build in memory so may be quicker than tsc itself when code changing frequently. (Just like webpack-dev-server)

Well, nodemon and onchange all work by doing a full-restart anyway. There's no hot-reloading or compilation step for node. So it is a full execution anyway. If anything, using tsc -w with onchange is probably quickest as the tsc compilation stays in memory while node does it's thing like usual.

For anyone who might come here for a nodemon solution as me.

Add these 2 property in nodemon.json file:

```json
"ext": "ts,yaml,js,json",
"execMap": {
"ts": "ts-node"
},
````

And then you can use nodemon app.ts smoothly.

Be careful for the generated .js file from tsc , they will overlay ts files when ts-node requires modules.

onchange -i -v -- ts-node index.ts also works~

Specifically for this issue I've created the tsc-watch library. you can find it on npm.

Obvious use case would be:

tsc-watch server.ts --outDir ./dist --onSuccess "node ./dist/server.ts"

This way the tsc is watching for changes and compiling only the changed files (speed!), and only on successful compilation it will run the command.

may also try to use this version of node-dev + ts-node it is faster then tsc --watch + node or some nodemon -x "ts-node server.ts" variations.

Old thread but, @blakeembrey can you explain your set up?

I have been trying with "tsc -w" + node-dev and while it was good, "tsc -w" never picks up newly added files, I had to manually restart "tsc -w" in those cases (there's even a small package to detect and restart tsc in those cases but things are starting to look less elegant) but lately, I've switched to "nodemon" and "ts-node", so that if any file gets modified or added "ts-node" gets restarted, which is usually fast enough.

I also found there's a package called "ts-node-dev" that does automatically restart upon changes to files that have been "require"ed but since it doesn't pick up changes to files that have not been "require"ed at that point, it wasn't a perfect solution.

What would be the current best approach to detect any changes under specified directory, added, modified or deleted and then does a restart in the fastest way?
While "tsc -w" is fast at recompiling, since it requires 2 steps to get the new file deployed (tsc gets triggerered for compiling and the resulting .js triggers the node process restart), it's actually not faster than just restarting ts-node in my opinion.

@windware You're definitely right. Today with the fast transpile mode being default my answer is probably different, it'd be quickest to either use ts-node directly and have something restarting it - or use something like ts-node-dev.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Borewit picture Borewit  路  3Comments

sanex3339 picture sanex3339  路  4Comments

cibergarri picture cibergarri  路  3Comments

conordickinson picture conordickinson  路  4Comments

OliverJAsh picture OliverJAsh  路  3Comments