java-script:node-js:auto-reload-files-no-restart-needed
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java-script:node-js:auto-reload-files-no-restart-needed [2023/08/09 17:10] – removed - external edit (Unknown date) 127.0.0.1 | java-script:node-js:auto-reload-files-no-restart-needed [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Automatic reload node.js files ====== | ||
| + | |||
| + | We'll use nodemon for this. | ||
| + | <note warning> | ||
| + | Nodemon is a tool meant for development. It should not be used in a production environment. | ||
| + | </ | ||
| + | |||
| + | ===== Install ===== | ||
| + | |||
| + | 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. \\ \\ | ||
| + | |||
| + | ===== Usage ===== | ||
| + | |||
| + | ==== Use nodemon instead of node ==== | ||
| + | |||
| + | 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. | ||
| + | |||
| + | ==== Npm scripts ==== | ||
| + | |||
| + | 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: | ||
| + | < | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | To run it: | ||
| + | |||
| + | < | ||
| + | npm start | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
