Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run script on change in nodemon

Is it possible to run a script on change using nodemon?

Is it possible to do something like this: nodemon --watch src --exec yarn run my-script?

So, ideally I'd like to run a script only when there're changes in a src folder

like image 900
Le garcon Avatar asked Jul 07 '18 17:07

Le garcon


People also ask

How do I run a TS with Nodemon?

In your package. json file, create a start script. This will serve as the command that will be run and restarted by nodemon when a file changes. This passes the start script as the executable command to run for your project by nodemon.

How do I run node js files automatically?

Restart the application automatically The node command has to be re-executed in bash whenever there is a change in the application. To restart the application automatically, use the nodemon module. This local installation of nodemon can be run by calling it from within npm script such as npm start or using npx nodemon.


2 Answers

Just put quotes around your script that you want to execute:

nodemon --watch src --exec "yarn run my-script"
like image 173
user1627270 Avatar answered Sep 19 '22 06:09

user1627270


As indicated in the Nodemon docs you can add a restart event command in nodemon.json:

{
  "events": {
    "restart": "yarn my-script"
  }
}
like image 22
Alan Friedman Avatar answered Sep 22 '22 06:09

Alan Friedman