how to do linting using nodemon?
Solution 1
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.
Solution 2
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
.
Comments
-
DrEarnest almost 2 years
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 over 8 yearsI 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.
-
SzybkiSasza over 8 yearsI 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. -
Alexis Tyler over 8 yearsYou'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.
-
DrEarnest over 8 yearsI 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??
-
SzybkiSasza over 8 yearsYou have answer in my previous comment - just use
events restart
flag from nodemon and add console linting exec there. ESLint has CLI: 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 ;) -
DrEarnest over 8 yearsNot 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.
-
SzybkiSasza over 8 years
--fix
flag for CLI should do. For the rest problems, you should fix them manually. -
shriek almost 8 yearsWhy this isn't accepted answer is beyond me. This is exactly the answer you're looking for OP.
-
Oskar over 7 yearsAn 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.
-
SzybkiSasza over 7 yearsThis answer was written a long time ago and before I stumbled upon this article: medium.freecodecamp.com/… and completely switched to using npm commands. I've just adjusted answer with my current knowledge ;)