Kill your node process if any of the files that
you require
changes.
yarn add --dev @bonton/exit-on-change
# or
npm install --save-dev @bonton/exit-on-change
node -r @bonton/exit-on-change/dist/exit-on-change .
This imports dist/exit-on-change.js
before importing
your own code and executing.
import '@bonton/exit-on-change';
// OR
require('@bonton/exit-on-change');
Once this is added, pass an environment variable like
EXIT_ON_CHANGE=1 node .
import {exitOnChange} from '@bonton/exit-on-change';
exitOnChange(true);
// OR
require('@bonton/exit-on-change').exitOnChange(true);
This package uses pirates
to hook into
node's module import system and then users chokidar
to watch for changes in required files.
The process is not killed immediately but rather after
a 100ms
delay by default. See below on hwo to change
the delay.
This is done so that changes can accumulate so as not to trigger too many restarts.
.js
, .jsx
, .ts
, .tsx
, .json
, .mjs
, .cjs
Default Timeout: 100ms
Just set the EXIT_ON_CHANGE_TIMEOUT
environment
variable to the amount of time in milliseconds.
# if CLI only
EXIT_ON_CHANGE_TIMEOUT=500 node -r @bonton/exit-on-change/dist/exit-on-change .
# if imported in main file
EXIT_ON_CHANGE_TIMEOUT=500 node .
The second argument takes the time in milliseconds.
import {exitOnChange} from '@bonton/exit-on-change';
exitOnChange(true, 500);
// OR
require('@bonton/exit-on-change').exitOnChange(true, 500);
Please use your container orchestration tool's native restart functionality.
Add the following property to your service definition
version: '3'
services:
your-service:
...
restart: unless-stopped # <-- this one
...
Setting restart: always
is also valid, if your
use-case demands it.
nodemon
is great, but what if you are tired
of listing dependencies in nodemon.json
because
your dependencies are in the parent folder and that
node_modules
directory is shared among all the other
projects in your yarn workspace? That's why.
Your container orchestration tool like docker compose
is more than capable of restarting services, and it
probably does it better and cleaner too.
MIT