how to do linting using nodemon?

拟墨画扇 提交于 2019-12-09 09:25:56

问题


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"
  },

回答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.




回答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.




回答3:


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



来源:https://stackoverflow.com/questions/34588458/how-to-do-linting-using-nodemon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!