我不确定我正在使用的东西是否有错误,或者我只是在这里设置了错误,但是运行eslint src --fix
时我会从ESLINT中遇到此错误关于" eslint-plugin-@typescript-eslint"
我已经指定了 @typescript-eslint文档中列出的插件@typescript-eslint/eslint-plugin
)
我正在使用盖茨比和随附的打字稿插件。
错误
$ eslint src --fix
Oops! Something went wrong! :(
ESLint: 4.19.1.
ESLint couldn't find the plugin "eslint-plugin-@typescript-eslint". This can happen for a couple different reasons:
1. If ESLint is installed globally, then make sure eslint-plugin-@typescript-eslint is also installed globally. A globally-installed ESLint cannot find a locally-installed plugin.
2. If ESLint is installed locally, then it's likely that the plugin isn't installed correctly. Try reinstalling by running the following:
npm i eslint-plugin-@typescript-eslint@latest --save-dev
.eslintrc.js:
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
},
env: {
browser: true,
node: true,
es6: true,
'jest/globals': true,
},
plugins: ['@typescript-eslint', 'react', 'jest'],
extends: [
'standard',
'plugin:react/recommended',
'plugin:@typescript-eslint/recommended',
'plugin:jest/recommended',
'plugin:prettier/recommended',
// 'eslint-config-prettier', // must be last
'prettier/@typescript-eslint',
],
rules: {
'react/prop-types': 0,
'jsx-quotes': ['error', 'prefer-single'],
'react/no-unescaped-entities': 0,
},
settings: {
react: {
version: 'detect',
},
linkComponents: [
// Components used as alternatives to <a> for linking, eg. <Link to={ url } />
'Hyperlink',
{ name: 'Link', linkAttribute: 'to' },
],
},
}
package.json
{
"name": "jmulholland.com",
"description": "My personal website",
"license": "MIT",
"scripts": {
"dev": "gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve",
"lint": "eslint src --fix",
"prettier": "prettier "**/*.+(js|jsx|ts|tsx|json|css|md|mdx|graphql)"",
"format": "yarn prettier --write",
"type-check": "tsc --noEmit",
"validate": "yarn lint && yarn prettier --list-different"
},
"dependencies": {
"gatsby": "^2.1.4",
"gatsby-plugin-react-helmet": "^3.0.6",
"gatsby-plugin-styled-components": "^3.0.5",
"gatsby-plugin-typescript": "^2.0.10",
"gatsby-plugin-typography": "^2.2.7",
"gatsby-remark-prismjs": "^3.2.4",
"gatsby-source-contentful": "^2.0.29",
"gatsby-transformer-remark": "^2.3.0",
"prismjs": "^1.15.0",
"prop-types": "^15.7.2",
"react": "^16.8.2",
"react-dom": "^16.8.2",
"react-helmet": "^5.2.0",
"react-typography": "^0.16.18",
"styled-components": "^4.1.3",
"typography": "^0.16.18"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^1.4.2",
"@typescript-eslint/parser": "^1.4.2",
"babel-jest": "^24.1.0",
"babel-plugin-styled-components": "^1.10.0",
"babel-preset-gatsby": "^0.1.8",
"dotenv": "^6.0.0",
"eslint": "^4.19.1",
"eslint-config-prettier": "^4.1.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jest": "^22.3.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-react": "^7.11.1",
"eslint-plugin-standard": "^4.0.0",
"faker": "^4.1.0",
"husky": "^1.3.1",
"jest": "^24.1.0",
"lint-staged": "^8.1.5",
"prettier": "^1.16.4",
"typescript": "^3.3.3333"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
解决方案仅升级到最新版本的Eslint
将"root": true
添加到.eslintrc.json
帮助我。
"root": true
通常是一个很好的ESLINT实践,以表明此文件是项目使用的根级别,ESLINT不应超越此目录以超越配置文件。
https://typescript-eslint.io/getting-started#details
我刚刚在一个大的monorepo上遇到了这个问题,找到了两个解决方案,可以为我们解决:
{
"scripts": {
"lint": "eslint src --resolve-plugins-relative-to ."
}
}
如果您使用纱线工作空间,yarn run
也可以做到这一点:
{
"scripts": {
"lint": "yarn run eslint src"
}
}
对于任何可能面临此问题的未来读者,就我而言,我正在使用基于node:alpine
映像的多阶段Docker构建。多阶段构建旨在在应用程序的dependencies
和devDependencies
之间创建一个分离(在package.json
中)。
在创建我的Dockerfile
的某个时刻,在几个小时内进行了许多修改,我在Dockerfile
开始时添加了以下行:
ENV NODE_ENV production
这会导致npm
忽略devDependencies
软件包,这又导致ESLINT失败(因为未安装)。
我将环境变量声明移至我最初想要的最终(发行)构建阶段,然后npm
安装了所有必需的软件包,并成功运行了Eslint。
希望这能节省某人宝贵的时间。
我在Mac上的解决方案就像
- 转到Global Node_modules
cd /usr/local/lib/node_modules/
- 删除全局ESLINT
rm -rf eslint
- 再次运行命令
eslint index.js app storybook test --fix --ext .js,.ts,.tsx
- 利润
问题可能导致以下原因:
- 过时的Eslint。
- ESLINT同时在全球和本地安装。解决方案。
- 从项目文件夹中缺少
.eslintrc
配置文件。解决方案:npx eslint --init
- 您项目外的项目 - 父母文件夹中的其他
node_modules
文件夹。 - 您项目外的项目 - 父母文件夹中的其他
.eslintrc
。 - 错误的软件包配置。阅读更多。
可能导致此问题的另一个原因没有配置ESLint
。
在我的情况下,将.eslintrc.json
文件添加到根目录(使用正确配置)解决了问题。
由于我现在与eslint
遇到了同样的问题,所以我在这里发布了有关该问题的根本原因。
我安装了此eslint-nullish-coalescing
插件,该插件是eslint
的叉子,以使其无效。此插件更改了node_module.bineslint.cmd
文件的内容。
来自
@IF EXIST "%~dp0node.exe" (
"%~dp0node.exe" "%~dp0..eslintbineslint.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0..eslintbineslint.js" %*
)
to
@IF EXIST "%~dp0node.exe" (
"%~dp0node.exe" "%~dp0..eslint-nullish-coalescingbineslint.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0..eslint-nullish-coalescingbineslint.js" %*
)
因此,Eslint找不到适合其工作的插件,因此错误。
尽管OP不使用eslint-nullish-coalescing
软件包,但是如果有人面对此问题的任何人,我建议验证node_modules.bineslint.cmd
文件的内容。
在我的情况下,一个父目录中有另一个node_modules文件夹。
将软件包目录移动到没有其他node_modules文件夹中的路径中,删除了错误。
最近,当我尝试在2017年创建的项目中使用Eslint和Prettier时,我遇到了相同的问题ESLINT至版本8 。版本6 也有效,但版本7 可能会导致另一个错误。真的很奇怪。
然后我添加了一个新脚本:
./node_modules/eslint/bin/eslint.js ./ --ext .ts,.tsx --config .eslintrc.js --fix
和Eslint效果很好。您可以在.eslintrc.js
(ON)中添加一些配置,以让Eslint知道应该覆盖哪些文件并格式化哪些文件。
我遇到了这个错误'./src/**/''./test/'
将这些从脚本中删除后,工作开始通过。错误是误导的,并且花了很多时间来解决。