Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use npm scripts to run tsc -watch && nodemon --watch?

I'm looking for a way to use npm scripts to run tsc --watch && nodemon --watch at the same time. I can run these commands independently, but when I want run both of them, only the first one is executed. eg:

"scripts": {         "runDeb": "set NODE_ENV=development&& tsc --watch && nodemon --watch"   } 

tsc --watch is executed but nodemon is never called, and vice versa.

like image 360
Nicolas Dominguez Avatar asked Jul 08 '16 23:07

Nicolas Dominguez


People also ask

Can I use npm with TypeScript?

via npm. You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal.

What does npm run tsc do?

This command first downloads and installs the typescript npm package. tsc is the executable name of the TypeScript compiler. So, once typescript has been installed, the TypeScript compiler is invoked. index.

How do I run tsc TypeScript?

We can use the ts-node package to execute TypeScript files from the command line. Install it with npm or other package manager. After that, simply execute the TypeScript files with the command: ts-node filename.


2 Answers

I think what you want is something like this (my current setup):

"scripts": {     "compile": "tsc && node app.js",     "dev": "./node_modules/nodemon/bin/nodemon.js -e ts  --exec \"npm run compile\"" } 

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 will exec 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.

like image 79
AlterX Avatar answered Sep 22 '22 06:09

AlterX


I have been using AlterX's solution for a while now and it has worked perfectly, but I have found it to be rather slow. Instead, I am now using tsc-watch. It makes tsc use incremental compilation similar to the -w flag, making the restart of the application much faster.

It's as easy as putting something similar to this in your package.json:

"scripts": {   "start": "tsc-watch --onSuccess \"node .\"" } 
like image 24
Borre Mosch Avatar answered Sep 22 '22 06:09

Borre Mosch