简体   繁体   English

Eslint precommit husky hook在create-react-app项目中未按预期工作

[英]Eslint precommit husky hook is not working as expected in create-react-app project

Here's my configuration 这是我的配置

package.json package.json

{
    "name": "hello-world",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "react": "^16.8.6",
        "react-dom": "^16.8.6",
        "react-scripts": "3.0.1"
    },
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
    },
    "husky": {
        "hooks": {
            "pre-commit": "lint-staged"
        }
    },
    "lint-staged": {
        "*.js": [
            "eslint --fix",
            "git add"
        ],
        "*.{js, jsx, json, html, css}": [
            "prettier --write",
            "git add"
        ]
    },
    "eslintConfig": {
        "extends": "react-app"
    },
    "browserslist": {
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    },
    "devDependencies": {
        "eslint": "^5.16.0",
        "eslint-config-prettier": "^6.0.0",
        "eslint-plugin-prettier": "^3.1.0",
        "husky": "^3.0.0",
        "lint-staged": "^9.2.0",
        "prettier": "^1.18.2"
    }
}

Now if I run that simply on the project like, ./node_modules/.bin/eslint . 现在,如果我仅在./node_modules/.bin/eslint .类的项目上运行它./node_modules/.bin/eslint . , it is working as usual. ,它照常工作。 Displays the changes that need to be made in the file but does not work as a pre-commit hook, prettier is working though. 显示需要在文件中进行的更改,但不能作为预提交挂钩使用,但更漂亮。 I still see that the eslint is running before every commit from the command line but I don't see any output presented. 我仍然看到eslint在每次命令行提交之前都在运行,但是没有看到任何输出。 What could be the issue? 可能是什么问题?

  • Changing the line to, eslint * --fix , shows me fixes that are to be made in all files, even .css and .md files. 将行更改为eslint * --fix ,向我显示了将在所有文件(甚至.css.md文件)中进行的修复。
  • Making it eslint *.js --fix tells me, no files matching the pattern were found. 使其成为eslint *.js --fix告诉我,没有找到与该模式匹配的文件。
  • Even eslint . --fix 甚至eslint . --fix eslint . --fix doesn't work. eslint . --fix不起作用。

My folder structure is a simple create-react-app format, if that info is needed. 如果需要该信息,我的文件夹结构是一种简单的create-react-app格式。 Full repo here 完整回购在这里

I'll post the eslintrc and prettierrc file below as well, 我还将在下面发布eslintrc和prettierrc文件,

.prettierrc .prettierrc

{
"singleQuote": false
}

.eslintrc .eslintrc

{
"extends": ["react-app", "prettier"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}

Clarifications: 说明:

1) I had to use the longer format version of prettier configuration, as opposed to plugin:prettier/recommended because I ran into this error while trying to have a separate prettierrc config file instead of having it inline in package.json 1)我必须使用更长格式的漂亮配置,而不是plugin:prettier/recommended因为我在尝试拥有一个单独的prettierrc配置文件而不是将其内嵌在package.json中时遇到了此错误

2) I had to use a lower version of eslint because the newer version was throwing random errors from a plain create-react-app and hence had to downgrade it as discussed here . 2)我必须使用较低版本的eslint,因为较新版本的eslint从普通的create-react-app抛出随机错误,因此必须对其降级,如此处所讨论

Did you try to use the complete path of the said linters in your package.json ? 您是否尝试在package.json中使用上述短毛绒的完整路径

Something like this - 像这样-

"lint-staged": {
    "*.{js,tsx}": [
        "./node_modules/.bin/eslint --fix",
        "git add"
    ],
    "*.{js, jsx, json, html, css}": [
        "./node_modules/.bin/prettier --write",
        "git add"
    ]
}

For me, it works as expected. 对我来说,它按预期工作。

Reference Tutorial 参考教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM