简体   繁体   English

为什么 create-react-app 在尝试 eslint 时会给出 typescript 错误(尽管没有 typescript 文件)?

[英]Why is create-react-app giving typescript error when trying to eslint (though no typescript files)?

I am getting this error when i do eslint.当我做eslint.

Error: Failed to load parser '@typescript-eslint/parser' declared in '.eslintrc.yml » 
eslint-config-react-app#overrides[0]': Cannot find module 'typescript'

Because there are TypeScript files - although ESLint excludes node_modules/ from linting by default, CRA's override (in order to load appropriate plugins for any TypeScript files, this is why you see eslint-config-react-app#overrides[0] in the output) does include the content of node_modules/ (see eg this issue ).因为TypeScript 文件 - 尽管 ESLint 默认排除node_modules/从 linting 中排除,但 CRA 的覆盖(为了为任何 TypeScript 文件加载适当的插件,这就是为什么你在输出中看到eslint-config-react-app#overrides[0] ) 确实包括node_modules/的内容(参见例如这个问题)。

You can install TypeScript, but if you don't intend to use any TypeScript files in your own source you can be more specific about what you want to lint instead.可以安装 TypeScript,但如果您不打算在您自己的源代码中使用任何 TypeScript 文件,您可以更具体地说明您想要 lint 的内容。 Either:任何一个:

  1. Change the script in package.json to only lint the source directory:package.json中的脚本更改为仅对源目录进行 lint:

     "lint": "eslint src/"
  2. Create a .eslintignore (or another file, if you set the right --ignore-path ) file containing:创建一个.eslintignore (或另一个文件,如果您设置了正确的--ignore-path )文件,其中包含:

     node_modules/
  3. Add to the ESLint configuration object in package.json (or pass it as an --ignore-pattern ):在 package.json 中添加 ESLint 配置package.json (或将其作为--ignore-pattern传递):

     "eslintConfig": { "extends": "react-app", "ignorePatterns": [ "node_modules/" ] }

The latter two options are from the documentation on configuration .后两个选项来自配置文档

Install typescript安装typescript

npm install --save-dev typescript

Then eslint.然后eslint.

will work as intended and can use the .eslintrc.yml file.将按预期工作并且可以使用.eslintrc.yml文件。

btw I had also just installed顺便说一句,我也刚刚安装

npm install -save-save eslint-plugin-typescript

in case you are doing this as you may need that too.如果您正在这样做,您可能也需要这样做。

btw do NOT install eslint with npm install eslint... as it may mess up due to the fact that... crewate-react-app already has it (but a lower version).顺便说一句,不要使用 npm 安装 eslint 安装 eslint... 因为它可能会因为...Crewate-react-app 已经拥有它(但版本较低)而搞砸。 so just add your eslintrc.yml file and it will work (be applied in the editor, and with this fix, at the command line as neeeded).所以只需添加您的 eslintrc.yml 文件,它就可以工作(在编辑器中应用,并根据需要在命令行中使用此修复程序)。

example output:例如 output:

.../src/components/SignUp/index.js
   1:41  error  Missing semicolon                              semi
   7:26  error  Unnecessary parentheses around expression      no-extra-parens
  15:2   error  Missing semicolon                              semi
  32:13  error  'username' is assigned a value but never used  no-unused-vars
  47:63  error  Missing semicolon                              semi
  95:26  error  Unnecessary parentheses around expression      no-extra-parens

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

相关问题 Typescript 在 create-react-app 中 EXTEND_ESLINT=true 时解析错误 - Typescript parsing error when EXTEND_ESLINT=true in create-react-app 使用打字稿模板将 create-react-app 更新到 4.0 时出错 - Error when updating create-react-app to 4.0 with typescript template 关于“create-react-app --template typescript”如何与 typescript-eslint 连接的困惑 - Confusion about how `create-react-app --template typescript` hooked up with typescript-eslint 尝试使用 create-react-app 开玩笑地运行打字稿 - trying to run typescript with jest with create-react-app create-react-app with --template typescript 不创建.tsx 文件 - create-react-app with -–template typescript not creating .tsx files create-react-app 和 TypeScript 错误:使用 JSX 时“React”必须在范围内 - create-react-app and TypeScript error: 'React' must be in scope when using JSX NPX create-react-app 找不到 @typescript-eslint/experimental-utils@2.19.1 的匹配版本 - NPX create-react-app cannot find a matching version for @typescript-eslint/experimental-utils@2.19.1 'npx create-react-app [name] --template typescript' 错误 - 'npx create-react-app [name] --template typescript' error 将 typescript 添加到 create-react-app 时出现 tslint 问题 - tslint problem when adding typescript to create-react-app 使用typescript将sass加载到create-react-app中 - loading sass in a create-react-app with typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM