14

Can I use nodemon to lint my javascript? I am not using any build tool e.g. gulp or grunt and want to maximize the use of node and npm.

The output from nodemon can be piped. I want to use this for linting the changed file using eslint.

Here is my package.json

{
  "name": "app",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "nodemon server.js",
    "lint": "eslint"
  },
  "dependencies": {
    "MD5": "*",
    "clean-css": "*",
    "express": "~4.9.0",
    "express-handlebars": "~2.0.1",
    "express-redis-cache": "*",
    "foundation-sites": "~5.5.3",
    "fs-extra": "~0.8.1",
    "node-rest-client": "~1.5.1",
    "node-sass": "*",
    "path": "*"
  },
  "devDependencies": {
    "babel-eslint": "^4.1.6",
    "eslint": "^1.10.3",
    "eslint-config-airbnb": "^2.1.1",
    "eslint-config-airbnb-es5": "^1.0.8",
    "eslint-plugin-react": "^3.13.1",
    "nodemon": "~1.8.1",
    "parallelshell": "~2.0.0",
    "watch": "~0.17.1"
  }
}

I tried this. But it doesn't work.It gives error.

       "scripts": {
    "start": "nodemon({ script: 'server.js' }).on('restart', function () {console.log('nodemon started');}).on('crash', function () {console.log('script crashed for some reason');});",
    "lint": "eslint"
  },
DrEarnest
  • 853
  • 2
  • 13
  • 27
  • 1
    Is there any particular reason you want to perform linting at runtime with a process manager rather than directly through your text editor? – Seonixx Jan 04 '16 at 10:42

3 Answers3

50

You can use the exec option of nodemon to run your tests as you save code. Here's an example:

nodemon server.js --exec 'npm run test && node'

This will cause nodemon to run npm run test && node server.js, and it won't start the server until all the tests have run successfully.

Brandon
  • 546
  • 4
  • 3
  • Why this isn't accepted answer is beyond me. This is exactly the answer you're looking for OP. – shriek Jul 05 '16 at 05:05
7

I use Standard.js for linting and I can get it to work with nodemon using the below package.json script.

"scripts": {
  "lint": "standard",
  "dev": "nodemon ./app --exec \"npm run lint && node\""
  "start": "nodemon ./app"
}

When I run npm run dev, any changes I make will be monitored and linted. I tested this in Windows using PowerShell.

cglotr
  • 884
  • 11
  • 16
-7

Linting is purely a development process. Nodemon is a tool used for running server and not connected with build tools, but with running your application. Therefore answer is "NO". You should use proper tool for development automation like grunt, gulp etc. or just pure NPM scripts (in your package.json file).

I'd recommend using automation tools if your project is complicated, has many stages and a lot of bundling etc. However, for only linting, you could just prepare one-liner in your npm package.json, e.g. like this:

"scripts": {
    "lint": "eslint --fix src test",
}

And then run it with command: 'npm run lint'.

Remember that by using properly configured .eslintrc file, you could lint your code both by console commands, but by editors/IDEs as well (almost every broadly popular IDE has plugin for eslint).

More info about configuring eslint and creating proper eslintrc file could be found here: http://eslint.org/docs/user-guide/configuring

SzybkiSasza
  • 1,591
  • 12
  • 27
  • I am using nodemon in development to watch over my javascript files. I am not going to use it in production and it has been listed in dev-dependencies. I guess this is correct use of nodemon and since it watch over all the javascript files in my app, I feel there must be a way to use it to monitor whether I am writing correct code or not. That's why I want to extend it for linting. – DrEarnest Jan 04 '16 at 10:56
  • 1
    I refined my answer to be more proper :) Main nodemon use is to run/restart server when files are changed during development, but not for building. If you want just to add output from eslinter to console (a bit impractical in my opinion), you could probably use `events restart` config flag in nodemon config file and add eslint there. – SzybkiSasza Jan 04 '16 at 11:01
  • You'd also usually use something like grunt or gulp watch to do exactly what you're using nodemon to do. Plus you'd then have the advantage of being able to automatically process your files to compress them, download assets that are needed, lint your files, etc. – Alexis Tyler Jan 04 '16 at 11:02
  • I am not using nodemon to build my file. As you say "Main nodemon use is to run/restart server when files are changed during development, but not for building", I just want to extend this to linting while checking for changes. Is this too much to ask for?? – DrEarnest Jan 04 '16 at 11:07
  • You have answer in my previous comment - just use `events restart` flag from nodemon and add console linting exec there. ESLint has CLI: http://eslint.org/docs/user-guide/command-line-interface.html Please don't feel offended by my comments and solution in any way, I was only trying to steer you into good coding practices and recommend best solution ;) – SzybkiSasza Jan 04 '16 at 11:31
  • Not offended at all. I am trying to use nodemon api as you suggested. I am able to use eslint through cli. But wanted to automate it, to enforce linting while developing. – DrEarnest Jan 04 '16 at 11:48
  • `--fix` flag for CLI should do. For the rest problems, you should fix them manually. – SzybkiSasza Jan 04 '16 at 12:23
  • An explanation for my harsh downvote: forcing your co developers and yourself to have a proper linted code could be something really powerful for maintaining a good code standard (learnt the hard way...). Node itself (and Nodemon) is really capable when it comes to modern JS (ES5+) and as long as your code can be run natively by Node there really is no reason to use anything else than npm scripts for ie an API or a microservice with nothing else than just pure Javascript.To bring in Gulp in that case would be like trying a crack a nut with a sledge hammer, a bit overkill and unnecessary. – Oskar Oct 22 '16 at 05:42
  • This answer was written a long time ago and before I stumbled upon this article: https://medium.freecodecamp.com/why-i-left-gulp-and-grunt-for-npm-scripts-3d6853dd22b8#.thly456kd and completely switched to using npm commands. I've just adjusted answer with my current knowledge ;) – SzybkiSasza Oct 24 '16 at 14:13