0

I have been following several SO links on how to run both tsc -w and nodemon app.js in a single command.

Link which i followed: How do I execute typescript watch and running server at the same time?

I was working on a project in node.js with typescript. Each time when there is change in ts code, I want to compile using tsc -w and then execute running server with nodemon app.js file. The problem is I tried with both commands, but still it is not running the second command. I have no idea why it is not working.

Commands I used inside package.json scripts

"dev1": "tsc -w && nodemon ./.build/src/app.js",

"dev2": "tsc && concurrently \"tsc -w\" \"nodemon ./.build/src/app.js\""

Ran npm run dev1

Ran npm run dev2

Currently only the first command is running and I am not able to see the logs which will come when nodemon runs. Terminal is showing only below log:

Found 0 errors. Watching for file changes

[Note: both commands are running perfectly when i tried to run one after another]

Versions using (bit older)

typescript: 3.0.1

nodemon: 2.0.7

ts-node: 5.0.1

Any help would be really appreciated. I dont want to use any extra package like ts-node-dev.

2 Answers2

0

There are a few things to check (and double check)

  • make sure you have installed the typescript package (and not the tsc package). do npm uninstall tsc; npm i -D typescript
  • make sure you run the tsc command correctly, e.g. tsc -w app.ts (or create a detailed tsconfig.json if you have a whole source directory
  • make sure you are giving the right path to the nodemon

This is a working script to get started with:

mkdir my-ts-project
cd my-ts-project
npm init -y
npm i -D concurrently typescript nodemon
vim app.ts
npx concurrently "npx tsc -w app.ts" "nodemon app.js"

and then you can change app.ts and see changes reflected.


Tip: I suggest using ts-node-dev instead of nodemon and use tsc only for production build. It is easier.

Ardeshir Izadi
  • 955
  • 1
  • 7
  • 25
0

For those who are still struggling to get a solution finally I came across a solution from Is there a way to use npm scripts to run tsc -watch && nodemon --watch?

Add the below script to package.json

"dev": "./node_modules/nodemon/bin/nodemon.js -e ts  --exec \"tsc && node ./.build/src/index.js\""

Once you made the code changes simply run the dev script with npm run dev which starts nodemon and makes it watch .ts files (using -e flag). Then, every time a .ts file changes nodemon will exec the task which basically compiles and runs the node app.