261

I'm trying to run a dev server with TypeScript and an Angular application without transpiling ts files every time. I found that I can do the running with ts-node but I want also to watch .ts files and reload the app/server as I would do with something like gulp watch.

abraham
  • 41,605
  • 9
  • 84
  • 134
Ieltxu Algañarás
  • 2,646
  • 2
  • 8
  • 7

12 Answers12

565

EDIT: Updated for the latest version of nodemon!

I was struggling with the same thing for my development environment until I noticed that nodemon's API allows us to change its default behaviour in order to execute a custom command.

For example, for the most recent version of nodemon:

nodemon --watch "src/**" --ext "ts,json" --ignore "src/**/*.spec.ts" --exec "ts-node src/index.ts"

Or create a nodemon.json file with the following content:

{
  "watch": ["src"],
  "ext": "ts,json",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "ts-node ./src/index.ts"      // or "npx ts-node src/index.ts"
}

and then run nodemon with no arguments.

By virtue of doing this, you'll be able to live-reload a ts-node process without having to worry about the underlying implementation.

Cheers!


And with older versions of nodemon:

nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/index.ts

Or even better: externalize nodemon's config to a nodemon.json file with the following content, and then just run nodemon, as Sandokan suggested:

{ "watch": ["src/**/*.ts"], "ignore": ["src/**/*.spec.ts"], "exec": "ts-node ./index.ts" }
Karl Horky
  • 3,362
  • 27
  • 33
HeberLZ
  • 8,576
  • 4
  • 20
  • 24
  • if `index.ts` is a express instance, how can i kill it and restart – hjl Aug 23 '16 at 08:41
  • @elaijuh in theory this same command should do the trick, when nodemon is configured to execute a custom command (in this case ts-node) instead of the default node command, it will shut down the process and start a new one each time it finds a change on the watch expression minus the ignore expression :) – HeberLZ Aug 24 '16 at 08:21
  • 18
    you can also create a nodemon.json file with all the mentioned options in it like this: `{ "watch": ["src/**/*.ts"], "ignore": ["src/**/*.spec.ts"], "exec": "ts-node ./app-server.ts" }` and just type `nodemon` – Sandokan El Cojo Oct 27 '16 at 08:27
  • 3
    I made the mistake of adding `./` before the folder names and it broke. This worked for me: `{ "verbose": true, "watch": ["server/**/*.ts"], "ext": "ts js json", "ignore": ["server/**/*.spec.ts"], "exec": "ts-node index.ts" }`. And command line: `nodemon --watch server/**/*.ts --ignore server/**/*.spec.ts --verbose --exec ts-node index.ts` – Adrian Moisa Mar 11 '17 at 10:26
  • 3
    I would just like mention, that you also have to set the `ext` in the config file, so its look for ts changes. My config file look like this: `{ "watch": ["src/**/*.ts"], "ignore": ["src/**/*.spec.ts"], "ext": "ts js json", "_exec": "node dist/startup.js", "exec": "ts-node src/startup.ts" }` – Lasse D. Slot Mar 28 '17 at 08:45
  • For windows users you will need to make the exec command an array for pathing issues: `"exec": "ts-node ./src/index.ts"` becomes => `"exec": ["ts-node", "src/index.ts"]` – Armeen Harwood Sep 10 '17 at 18:34
  • I'm using grunt-ts with nodemon, what is the difference?.. Should I use grunt-ts or ts-node..? – danger89 May 15 '18 at 18:15
  • @HeberLZ `nomdemon.json` should be `nodemon.json`, i think – Simon Meusel Jun 02 '18 at 17:23
  • nodemon.json file makes things easier, thanks for sharing. – darul75 Feb 15 '19 at 11:16
  • still the best practice? – SuperUberDuper Jun 07 '19 at 16:21
  • I am getting (function (exports, require, module, __filename, __dirname) { import * as express from 'express'; SyntaxError: Unexpected token * looks like it is not taking es6 code, how can I add es6? – Pritam Bohra Aug 06 '19 at 14:32
  • I have the same config as you but it only refreshes when i update index.ts and not any of the javascript files in src. – Batman Aug 19 '19 at 17:44
  • `nodemon` and t`s-node` will become very slow if the typescript project goes too big. `ts-node-dev` can not reload when a ts file only contains interfaces got changes. – Ling Oct 17 '19 at 07:50
  • 1
    On Windows machines, DON'T use single quotes in your package.json. Replacing those with `\"` makes the script run fine: `"nodemon --watch \"./src/**/*.ts\" -r dotenv/config --exec \"ts-node\" src/index.ts"` – TJBlackman Mar 11 '20 at 15:27
  • with first `nodemon.json` version I had to run `nodemon -e ts`, now with updated config file this option is not required. – humkins Mar 17 '20 at 14:07
180

I've dumped nodemon and ts-node in favor of a much better alternative, ts-node-dev https://github.com/whitecolor/ts-node-dev

Just run ts-node-dev src/index.ts

Mikael Couzic
  • 9,003
  • 4
  • 20
  • 16
63

Here's an alternative to the HeberLZ's answer, using npm scripts.

My package.json:

  "scripts": {
    "watch": "nodemon -e ts -w ./src -x npm run watch:serve",
    "watch:serve": "ts-node --inspect src/index.ts"
  },
  • -e flag sets the extenstions to look for,
  • -w sets the watched directory,
  • -x executes the script.

--inspect in the watch:serve script is actually a node.js flag, it just enables debugging protocol.

im.pankratov
  • 1,380
  • 1
  • 12
  • 15
36

This works for me:

nodemon src/index.ts

Apparently thanks to since this pull request: https://github.com/remy/nodemon/pull/1552

DLight
  • 808
  • 9
  • 17
  • This works for me too but how? Seems kind of magical. What's compiling the typescript? I don't have `ts-node` installed. – d512 Dec 11 '19 at 21:18
  • 1
    @d512 Are you sure it's not in your `node_modules/`? For me it fails if I don't have it. – DLight Dec 11 '19 at 21:41
  • 1
    This indeed does require `ts-node` to be installed. Running this command without `ts-node` will result in an `failed to start process, "ts-node" exec not found` error. You likely had this as a leftover artifact in `node_modules`. That being said, this solution is much nicer since it doesn't require additional config. – Brandon Clapp May 25 '20 at 20:45
  • Install ts-node globally: `npm install -g ts-node` – Rafael Pizao Sep 09 '20 at 21:26
20

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.js"

Eliasz Kubala
  • 3,414
  • 1
  • 18
  • 27
gilamran
  • 5,914
  • 4
  • 27
  • 48
  • How would this work in the case of an express or koa server since it doesn't actually kill the previous node instance? – brianestey Nov 27 '18 at 03:55
  • 'tsc-watch' kills and restarts the process for you. – gilamran Nov 28 '18 at 17:10
  • This is exactly what I was looking for. Not sure what the purpose of ts-node-dev is, but I couldn't get it to report typescript errors. After spending hours trying to get it working, I tried tsc-watch, and it worked like a charm! – Charles Naccio Jan 16 '20 at 08:04
  • @gilamran in the documentation of your package there is a typo: `"[...] similar to nodemon but for TypeCcript."`:) – Massimiliano Kraus Jan 27 '20 at 22:26
15

you could use ts-node-dev

It restarts target node process when any of required files changes (as standard node-dev) but shares Typescript compilation process between restarts.

Install

yarn add ts-node-dev --dev

and your package.json could be like this

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "tsc": "tsc",
  "dev": "ts-node-dev --respawn --transpileOnly ./src/index.ts",
  "prod": "tsc && node ./build/index.js"
}
jsina
  • 2,973
  • 24
  • 21
13

Add "watch": "nodemon --exec ts-node -- ./src/index.ts" to scripts section of your package.json.

takasoft
  • 1,030
  • 13
  • 15
8

i did with

"start": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec ts-node src/index.ts"

and yarn start.. ts-node not like 'ts-node'

4

add this to your package.json file

scripts {
"dev": "nodemon --watch '**/*.ts' --exec 'ts-node' index.ts"
}

and to make this work you also need to install ts-node as dev-dependency

yarn add ts-node -D

run yarn dev to start the dev server

princebillyGK
  • 987
  • 11
  • 13
4

I would prefer to not use ts-node and always run from dist folder.

To do that, just setup your package.json with default config:

....
"main": "dist/server.js",
  "scripts": {
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "dev": "nodemon"
  },
....

and then add nodemon.json config file:

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

Here, i use "exec": "npm restart"
so all ts file will re-compile to js file and then restart the server.

To run while in dev environment,

npm run dev

Using this setup I will always run from the distributed files and no need for ts-node.

Raja P.B.
  • 51
  • 4
1

Another way could be to compile the code first in watch mode with tsc -w and then use nodemon over javascript. This method is similar in speed to ts-node-dev and has the advantage of being more production-like.

 "scripts": {
    "watch": "tsc -w",
    "dev": "nodemon dist/index.js"
  },
0

If you are having issues when using "type": "module" in package.json (described in https://github.com/TypeStrong/ts-node/issues/1007) use the following config:

{
  "watch": ["src"],
  "ext": "ts,json",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "node --loader ts-node/esm --experimental-specifier-resolution ./src/index.ts"
}

or in the command line

nodemon --watch "src/**" --ext "ts,json" --ignore "src/**/*.spec.ts" --exec "node --loader ts-node/esm --experimental-specifier-resolution src/index.ts"
fernandopasik
  • 7,364
  • 5
  • 41
  • 52