Re-run build script whenever code changes

Viewed 12

So in my codebase I have a server.js file which is an express app that serves the index.html of dist folder.

I also have a build script which is a webpack script to build a react application which looks something like this webpack --mode=development --out-dir dist

The issue is I have to do npm run build and then do node server.js each time I change something in the code to see my latest changes.

I want to enable hot reloading.

I tried to install watch package. and include a script in package.json but that does not work unfortunately.

scripts: {
 "watch":"watch 'npm run build' ./dist"
}

I know If I could create a build everytime code changes I can listen to server using nodemon, but I am unsure as to how I could achieve it.

And better way to do this ??

1 Answers

I added a watch build script and which updates my dist folder whenever the code changes happens.

scripts: {
"build-watch": "webpack --mode=development --out-dir dist --watch"
}

After this I run a nodemon server.js so that my new changes in dist folder are picked by express server and served locally.

Related