Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do linting using nodemon?

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"
  },
like image 515
DrEarnest Avatar asked Jan 04 '16 09:01

DrEarnest


People also ask

What is Linting in node JS?

A linter is a computer program that analyzes and checks source code. It flags programming errors, indentation errors, formatting errors, bugs, and suspicious constructs.

What is the purpose of using Nodemon?

Nodemon is a popular tool that is used for the development of applications based on node. js. It simply restarts the node application whenever it observes the changes in the file present in the working directory of your project.

What is the difference between Nodemon and node?

nodemon is a tool that helps develop node. js based applications by automatically restarting the node application when file changes in the directory are detected. To use nodemon, replace the word node on the command line when executing your script.


2 Answers

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.

like image 180
Brandon Avatar answered Oct 28 '22 07:10

Brandon


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.

like image 39
cglotr Avatar answered Oct 28 '22 07:10

cglotr