I'm looking for a way to use npm scripts to run tsc --watch && nodemon --watch
at the same time. I can run this commands independently, but when I want run both of them, only the first one is executed.
eg.
if I have this script:
"scripts": {
"runDeb": "set NODE_ENV=development&& tsc --watch && nodemon --watch"
}
tsc --watch
is executed but nodemon
is never called, and vice versa.
I think what you want is something like this (my current setup):
I created two scripts "compile" and "dev". To start developing you simply run
npm run dev
which starts nodemon and makes it watch .ts files (using the-e
flag). Then, every time a .ts file changes nodemon willexec
the compile task which basically compiles and runs the node app.While using concurrently is a good option, my setup guarantees that
tsc
's work is done before attempting to execute the resulting .js files.What's going on
The problem is there are two watchers here on all the files. One is
tsc -w
and one isnodemon
.When a change to a
.ts
file is made,tsc
detects that, compiles it, and creates the.js
version in your destination folder.Now from the Nodemon's perspective, it detects two changes (at least) -- one for
.ts
and one for.js
. On the first change it restarts itself, but on the second change it doesn't know that another "start" is going on already, so it tries to restart again and it fails. To me it's a nodemon bug -- see https://github.com/remy/nodemon/issues/763.Solutions
1) Use
tsc-watch --onSuccess
tsc-watch has
--onSuccess
which you can putnode
on there. This way you will have only one watcher.2) Delay nodemon
You can easily delay nodemon restarts (See --delay). It requires the least set up change.
3) Have nodemon only monitor destination folder of TSC
I couldn't get it to set up, but this way
nodemon
will detect only one change hopefully. It might cause problems in future or when tsc generates multiple files.The
TypeScript-Node-Starter
is fasthttps://github.com/microsoft/TypeScript-Node-Starter/blob/master/package.json
Here we are giving
npm run watch-ts
theTypeScript
name (by usingconcurrently -n
) and adding the coloryellow.bold
by using theconcurrently -c
.So, I can recognize pretty easy the messages for each process.
Normal compilation is: if file name is main.ts
step 1: tsc main.ts
step 2: node main.js
Simple and Onetime(loop) compilation:
tsc main --watch
Try to add this to your package.json:
And also add this npm packages (concurrently, nodemon, typescript) to your package.json:
My solution in october 2018 using newest versions of nodemon.
first:
install
nodemon
(npm install nodemon --save-dev
) andts-node
(npm install ts-node --save-dev
)second:
create a
nodemon.json
. I like to keep my nodemon config in a seperat nodemon.json to make the npm scripts a tad easier to read. So createnodemon.json
in the root of the project with the following content:then create your
npm start
script e.g like this:Then run
npm run dev:ts
oryarn dev:ts
should run and watch your typescript server code.For more configs like
Jest
units tests etc... you can take a look into this article