0

I have a project written in TypeScript and running on Node. I'm really struggling to write the script with npm to get it running for development. What I trying to do is:

  • clean the /dist folder
  • if a .ts change, compile it into /dist and restart node

Here is my 1st attempt, from the scripts section of my package.json:

"clean": "rimraf dist/**/*",
"build": "tsc",
"watch:start": "npm run clean && nodemon -e ts --exec \"npm run start\"",
"start": "npm run build && node dist/index.js"

If I start my project with npm run watch:start, it got stuck in a loop:

npm run watch:start
> nodemon -e ts --exec "npm run start"

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm run start`
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `npm run start`
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...

Here is my 2nd attempt, using npm-run-all to run several task in parallel:

"clean": "rimraf dist/**/*",
"build": "tsc",
"watch:start": "npm-run-all clean build --parallel --race watch:build watch:serve --print-label",
"watch:build": "tsc -w",
"watch:serve": "nodemon dist/index.js"

This one works better but it still restart node several time at startup.

Suggestions and improvements welcome !

2
  • You should first run tsc to compile your typescript app and run tsc -w and your server concurrently. Commented Jul 31, 2017 at 13:18
  • That what I do in my 2nd attempt. I added the "build" part after the "clean" in my 1st attempt and got the same result. "tsc -w" build all the .ts files when started anyway. Commented Jul 31, 2017 at 13:34

1 Answer 1

1

You can use tsc-watch, which omits the use of nodemon and runs when any changes affect the typescript source of your app.

"scripts": {
  "dev": "tsc-watch --onSuccess \"npm start\" ",
  "start": "node index.js"
}

It has a success handler --onSuccess which restarts the server everytime a change is made to the typescript source.

npm run dev

index.js

console.log('node run')

setTimeout(() => console.log(Math.random() * 1000.0), 1000);

console

npm run dev    
> tsc && concurrently "npm run node-tsc:w"

npm WARN invalid config loglevel="notice"

> [email protected] node-tsc:w C:\Users\Shane\Desktop\so
> tsc-watch --onSuccess "node index.js"

16:49:31 - Compilation complete. Watching for file changes.


node run
709.2226373633507
16:49:36 - File change detected. Starting incremental compilation...


16:49:37 - Compilation complete. Watching for file changes.


node run
974.6349162351444
16:49:41 - File change detected. Starting incremental compilation...


16:49:41 - Compilation complete. Watching for file changes.


node run
935.9043001938232

Node is restarted after changing the typecript source.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.