Skip to content

Commit

Permalink
docs: more docs for nodemon as child process (#1362)
Browse files Browse the repository at this point in the history
I was trying to find out how to use nodemon as child process, and stumble upon this #1018

So adding more docs for other people later to make it clearer how to do it.

[skip ci]
  • Loading branch information
andiwinata authored and remy committed Jun 14, 2018
1 parent 935e260 commit a724e53
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -125,6 +125,10 @@ Note that if you specify a `--config` file or provide a local `nodemon.json` any

Please see [doc/requireable.md](doc/requireable.md)

## Using nodemon as child process

Please see [doc/events.md](doc/events.md#Using_nodemon_as_child_process)

## Running non-node scripts

nodemon can also be used to execute and monitor other programs. nodemon will read the file extension of the script being run and monitor that extension instead of .js if there's no `nodemon.json`:
Expand Down
18 changes: 18 additions & 0 deletions doc/events.md
Expand Up @@ -47,11 +47,29 @@ nodemon.emit('restart');
nodemon.emit('quit');
```

## Using nodemon as child process

If nodemon is a spawned process, then the child (nodemon) will emit message
events whereby the event argument contains the event type, and instead of
emitting events, you `send` the command:

```js
// using `spawn` as example, can use other functions like `fork`, etc
// https://nodejs.org/api/child_process.html
const { spawn } = require('child_process');

function spawnNodemon() {
const cp = spawn('nodemon', ['path/to/file.js', '--watch', 'path/to/watch'], {
// the important part is the 4th option 'ipc'
// this way `process.send` will be available in the child process (nodemon)
// so it can communicate back with parent process (through `.on()`, `.send()`)
// https://nodejs.org/api/child_process.html#child_process_options_stdio
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
});

return cp;
}

var app = spawnNodemon();

app.on('message', function (event) {
Expand Down

0 comments on commit a724e53

Please sign in to comment.