【问题标题】:How to run eslint --fix from npm script如何从 npm 脚本运行 eslint --fix
【发布时间】:2017-03-09 08:21:21
【问题描述】:

我正在尝试在我的 package.json 中添加 lint-fix。 我的基本 lint 是 "lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"

我确实尝试了"lint-fix": "eslint --fix --ext .js,.vue src test/unit/specs test/e2e/specs",但它抛出了我的错误,但没有修复它们。

我需要改变什么?

来自 NPM 的错误是:

217 problems (217 errors, 0 warnings)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run-script" "lint-fix"
npm ERR! node v5.10.1
npm ERR! npm v3.8.3
npm ERR! code ELIFECYCLE
npm ERR! quasar-app@0.0.1 lint-fix: eslint --fix --ext .js,.vue src
npm ERR! Exit status 1
npm ERR! but it is caused by exit code 1 which is result of finding errors.

【问题讨论】:

  • 你能分享错误信息/堆栈跟踪吗?
  • @Gyandeep - 错误列表,217 个问题(217 个错误,0 个警告)npm ERR! Windows_NT 10.0.14393 npm 错误! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run-script" " lint-fix" npm 错误!节点 v5.10.1 npm 错误! npm v3.8.3 npm 错误!代码 ELIFECYCLE npm 错误! quasar-app@0.0.1 lint-fix: eslint --fix --ext .js,.vue src npm ERR!退出状态 1 npm ERR!但它是由退出代码1引起的,这是发现错误的结果。

标签: javascript npm eslint npm-scripts


【解决方案1】:

您可以运行以下命令:

npm run lint -- --fix

【讨论】:

  • 这 - 技巧适用于每个脚本吗?想知道我什么时候出现构建错误,建议运行 --stacktrace...
  • 你能解释一下这里的“--”是什么意思吗?
  • @Arhacktector 程序使用-- 作为停止从该点处理参数作为命令行选项的指示是很常见的,例如rm -- * 不会触发强制删除,即使有一个名为-f 的文件。因此,对于这个特定的命令行,-- 使 npm 命令不将--fix 解释为它应该解释的命令行选项,而是将它作为“原始”传递给运行子命令的程序。所以最终结果和目标是 --fix 参数被传递给 lint 而不是被 npm 消耗。
  • "npm run lint -- --fix" 解决了我的 EsLint 警告和错误问题
  • 这对我来说可以修复自动修复的 lint 错误。谢谢你:)
【解决方案2】:

您可以在 package.json 中添加新命令。

"scripts": {
    "lint": "eslint --fix --ext .js,.jsx ."
}

然后你就可以在终端npm run lint运行它了。

【讨论】:

    【解决方案3】:

    要添加一个新的单独脚本来自动修复扩展名为 .js.jsx 的文件的 linting 问题,您可以在 package.json 中添加添加脚本,如下所示:

    "lint:fix": "eslint --fix --ext .js,.jsx ."

    添加上述命令后,package.json 的脚本部分可能如下所示:

    ...
    ....
    "scripts": {
      "start": "react-scripts start",
      "build": "react-scripts build",
      "test": "react-scripts test",
      "eject": "react-scripts eject",
      "lint": "eslint .",
      "lint:fix": "eslint --fix --ext .js,.jsx ."
    },
    ....
    ...
    

    要运行新添加的脚本来自动修复命令行上的 linting 问题,您应该运行如下命令:

    npm run lint:fix

    【讨论】:

    • 我收到这个警告,我的问题是什么Warning: React version not specified in eslint-plugin-react settings. See https://github.com/yannickcr/eslint-plugin-react#configuration .
    【解决方案4】:

    试试

    ./node_modules/.bin/eslint --fix .
    

    【讨论】:

    【解决方案5】:

    这是我用的:

    npx eslint --fix . 
    

    它修复了所有文件。

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 2018-01-03
      • 2017-01-19
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 2016-07-18
      • 2021-07-02
      • 2021-08-14
      相关资源
      最近更新 更多