From 5a914cb35baffb981dfa327de3e91b4fad7876ee Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Fri, 29 Dec 2017 10:10:25 +0000 Subject: [PATCH] fix: handle exit(2) better --- README.md | 6 ------ faq.md | 12 +++++++++++- lib/config/exec.js | 3 ++- lib/monitor/run.js | 3 +-- lib/nodemon.js | 3 +++ package.json | 1 + 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 752783b8..dab4ee4c 100644 --- a/README.md +++ b/README.md @@ -324,12 +324,6 @@ Nodemon is not perfect, and CLI arguments has sprawled beyond where I'm complete See the [FAQ](https://github.com/remy/nodemon/blob/master/faq.md) and please add your own questions if you think they would help others. -## Contributors - -This project exists thanks to all the people who [contribute](https://github.com/remy/nodemon/blob/master/.github/CONTRIBUTING.md). -[![nodemon contributors](https://opencollective.com/nodemon/contributors.svg?width=890)](https://opencollective.com/nodemon#backer) - - ## Backers Thank you to all [our backers](https://opencollective.com/nodemon#backer)! 🙏 diff --git a/faq.md b/faq.md index ecb1d90d..efbd3497 100644 --- a/faq.md +++ b/faq.md @@ -26,6 +26,16 @@ $ nodemon app.js -- -L -opt2 -opt3 nodemon will ignore all script arguments after `--` and pass them to your script. +# Error: "process failed, unhandled exit code (2)" + +Nodemon will look for exit signals from the child process it runs. When the exit code is `2`, nodemon throws an error. Typically this is because the arguments are bad for the executing program, but it can also be due other reasons. + +For example, mocha@3.x will exit with `2` on failing tests. To handle the exit code in a way that nodemon can consume, manually exit the process, i.e.: + +```bash +nodemon -x 'mocha test/bad.test.js || exit 1' +``` + # Can't install nodemon: permission issue You may need to install nodemon using `sudo` (which isn't recommended, but I understand it's unavoidable in some environemnts). If the install fails with this appearing in the npm error log, then you need the following workaround. @@ -103,7 +113,7 @@ A workaround is to make sure that `node` binary exists in the `PATH`: sudo ln -s /usr/bin/nodejs /usr/local/bin/node ``` -Alternatively the `--exec nodejs` option can be used. +Alternatively the `--exec nodejs` option can be used. Fedora and Ubuntu pakage node as nodejs, because node.dpkg is diff --git a/lib/config/exec.js b/lib/config/exec.js index 2e53b46a..5d87814d 100644 --- a/lib/config/exec.js +++ b/lib/config/exec.js @@ -75,7 +75,8 @@ function exec(nodemonOptions, execMap) { var options = utils.clone(nodemonOptions || {}); - if (!options.script && options.args.length) { // try with the first argument + // if there's no script passed, try to get it from the first argument + if (!options.script && (options.args || []).length) { const script = expandScript(options.args[0], options.ext && ('.' + (options.ext || 'js').split(',')[0])); diff --git a/lib/monitor/run.js b/lib/monitor/run.js index b6b0e309..cc149131 100644 --- a/lib/monitor/run.js +++ b/lib/monitor/run.js @@ -143,8 +143,7 @@ function run(options) { if (code === 2) { // something wrong with parsed command - utils.log.error('failed to start process, possible issue with exec ' + - 'arguments'); + utils.log.error('process failed, unhandled exit code (2)'); bus.emit('error', code); process.exit(); } diff --git a/lib/nodemon.js b/lib/nodemon.js index c2f89734..c71e2e6e 100644 --- a/lib/nodemon.js +++ b/lib/nodemon.js @@ -138,6 +138,9 @@ function nodemon(settings) { if (!config.required) { const restartSignal = config.options.signal === 'SIGUSR2' ? 'SIGHUP' : 'SIGUSR2'; process.on(restartSignal, nodemon.restart); + utils.bus.on('error', () => { + utils.log.fail((new Error().stack)); + }); utils.log.detail((config.options.restartable ? 'or ' : '') + 'send ' + restartSignal + ' to ' + process.pid + ' to restart'); } diff --git a/package.json b/package.json index 158eab6c..d96deab4 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "clean": "rm -rf test/fixtures/test*.js test/fixtures/test*.md", "web": "node web", "semantic-release": "semantic-release pre && npm publish && semantic-release post", + "prepush": "npm run lint", "postinstall": "node -e \"console.log('\\u001b[32mLove nodemon? You can now support the project via the open collective:\\u001b[22m\\u001b[39m\\n > \\u001b[96m\\u001b[1mhttps://opencollective.com/nodemon/donate\\u001b[0m\\n')\"" }, "devDependencies": {