如何使用nodemon进行linting?

时间:2016-01-04 09:53:35

标签: javascript node.js npm nodemon eslint

我可以使用nodemon来lint我的javascript吗?我没有使用任何构建工具,例如gulp或grunt,并希望最大限度地使用node和npm。

The output from nodemon can be piped。我想使用它来使用eslint来修改已更改的文件。

这是我的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"
  }
}

我试过这个。但它没有用。它给出了错误。

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

3 个答案:

答案 0 :(得分:35)

您可以使用exec的{​​{1}}选项在保存代码时运行测试。这是一个例子:

nodemon

这将导致nodemon运行nodemon server.js --exec 'npm run test && node' ,并且在所有测试成功运行之前它不会启动服务器。

答案 1 :(得分:5)

我使用Standard.js进行linting,我可以使用下面的nodemon脚本将其与package.json一起使用。

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

当我运行npm run dev时,我所做的任何更改都会受到监控和修改。我使用WindowsPowerShell中对此进行了测试。

答案 2 :(得分:-5)

Linting纯粹是一个开发过程。 Nodemon是用于运行服务器而不与构建工具连接的工具,但是运行应用程序。因此答案是" NO"。您应该使用适当的工具进行开发自动化,例如grunt,gulp等,或者只使用纯NPM脚本(在package.json文件中)。

如果您的项目很复杂,有很多阶段和大量捆绑等,我建议使用自动化工具。但是,对于只有linting,您可以在npm package.json中准备一个单行程,例如像这样:

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

然后用命令运行它:' npm run lint'。

请记住,通过使用正确配置的.eslintrc文件,您可以通过控制台命令来控制您的代码,但也可以通过编辑器/ IDE(几乎每个广受欢迎的IDE都有插件用于eslint)。

有关配置eslint和创建正确的eslintrc文件的更多信息,请访问: http://eslint.org/docs/user-guide/configuring