We'll use nodemon for this.
To automatic load the files, without a restart being needed, we should install:
npm install --save-dev nodemon
The –save-dev flag saves nodemon as a development dependency, meaning it won't be installed in production environments.
Once installed, you can start your application with nodemon instead of node. For example, if you have a file server.js, you would start it with nodemon as follows:
npx nodemon server.js
This starts the server.js application and monitors for any changes.
If nodemon detects any changes in your source files, it will automatically restart your server.
You can also add nodemon as a start script in your package.json. This way, you can start your application with nodemon by running npm start.
Here is an example of how you would do this:
"scripts": { "start": "nodemon server.js" }
To run it:
npm start