noder: node + restart
Simple node script that allows you to run scripts which are able to
self-restart, self-terminate and run forever.

How does it work?
noder listens to special directives on stdout stream. When the script wants to
be restarted, it simply console.log a directive like [noder:restart]. Then
noder catches it and restarts the script. So simple, isn't it?
Installation
$ npm i -g noder-restart
$ yarn global add noder-restart
$ npm i -D noder-restart
$ yarn add -D noder-restart
Usage
Save this script as script.js:
console.log('Hello World!');
setTimeout(() => {
console.log('Restarting...');
console.log('[noder:restart]');
}, 1000);
Then run it using noder:
$ noder script.js
Or, if you installed noder locally:
$ npx noder script.js
$ yarn noder script.js
The script will be restarted every second.
console.log('[noder:restart]'); - restarts script
console.log('[noder:terminate]'); - terminates script
Safe console
You can enable safe console mode in the script run by noder. This mode prevents
terminate/restart by accident by removing the directives from the stream while
using console.log.
Here's how to enable safe console:
const { enableSafeConsole } = require('noder-restart');
const terminator = enableSafeConsole({
restart: '[noder:restart]',
terminate: '[noder:terminate]'
});
console.log('[noder:restart]');
console.log('[noder:terminate]');
terminator.restart();
Note: You should install noder as a normal (not dev) dependency if you want to
use safe console (don't add -D flag to the npm i/yarn add command).
CLI
Usage: noder [options] <script> [script args]
Options:
-h, --help - Displays noder's help
-v, --version - Displays noder's version
-f, --forever - Enables forever mode (it means the script will restart after crash/exit; you can quit it by terminating)
-w, --watch - Defines glob patterns for watch mode
-N, --no-node - Disables node script execution mode (you can execute every script/program)
-c, --config - Loads custom config file (the default one is /current/working/dir/noder.json if exists)
--restart - Defines custom restart directive
--terminate - Defines custom terminate directive
Config
By default noder searches for the config file named noder.json placed in the
current working directory (you can change it using -c/--config option).
Content:
{
"file": "script.js",
"args": ["arg1", "arg2"],
"forever": false,
"watch": ["*.js", "*.ts"],
"noNode": false,
"directives": {
"restart": "[noder:restart]",
"terminate": "[noder:terminate]"
}
}
file - node script to execute (or the program name/path if noNode is true)
args - CLI arguments for script/program
forever - forever mode (auto-restart after exit/crash)
watch - array with glob patterns for watch mode
noNode - disable node (runs every program; the path to program must be provided in file)
directives - list of directives
restart - restart directive
terminate - terminate directive