简体   繁体   English

Yarn 3.x PNP typescript CRA eslint 无法与 VSCode 一起使用

[英]Yarn 3.x PNP typescript CRA eslint not working with VSCode

I have been trying this for 9 hours now.我已经尝试了 9 个小时了。 I have gone through every single github issues, blogs, links I could find on inte.net and tried over hundred times.我已经浏览了每一个 github 问题、博客、我可以在 inte.net 上找到的链接,并尝试了一百多次。 Finally I am posting it here.最后我把它贴在这里。

I am using yarn berry 3.0.2.我正在使用 yarn berry 3.0.2。 I did yarn dlx create-react-app./ --template typescript .我做了yarn dlx create-react-app./ --template typescript So far so good.到目前为止,一切都很好。 Then I installed yarn vscode sdk to work with Zero-Installs PNP.然后我安装了 yarn vscode sdk 来使用零安装 PNP。 And added react + typescript + airbnb config.并添加了 react + typescript + airbnb 配置。 Linting also working but only from command line. Linting 也可以工作,但只能从命令行进行。 I just cannot get eslint vscode extension enabled.我只是无法启用 eslint vscode 扩展。 I always get我总是得到

o use ESLint please install eslint by running yarn add eslint in the workspace folder client
or globally using 'yarn global add eslint'. You need to reopen the workspace after installing eslint.

Alternatively you can disable ESLint for the workspace folder client by executing the 'Disable ESLint' command.

Now I do have eslint and all other stuffs installed.现在我确实安装了 eslint 和所有其他东西。 I think the problem is there is no node_modules folder because of yarn plug and play.我认为问题是没有 node_modules 文件夹,因为纱线即插即用。 How can I somehow configure vscode eslint extension to work.我怎样才能以某种方式配置 vscode eslint 扩展来工作。

Thank you.谢谢你。

I ran into this issue a bit ago and found that this is due to the module resolver not pointing to the correct directory.我之前遇到过这个问题,发现这是由于模块解析器没有指向正确的目录。 This is my workaround for VSCode , in order to do this in another IDE I would check out the documentation for the IDE-specific ESLint plugin and how to have the IDE target the yarn directory sdk.这是我的VSCode解决方法,为了在另一个 IDE 中执行此操作,我将查看特定于 IDE 的ESLint插件的文档以及如何让 IDE 以 yarn 目录 sdk 为目标。


The below steps are assuming the following:以下步骤假设如下:

  • You have installed ESLint on your IDE你已经在你的IDE上安装了 ESLint
  • You have installed ESLint via Yarn 2+你已经通过 Yarn 2+ 安装了ESLint
  • .yarnrc linker points to pnp .yarnrc linker 指向 pnp
{
   // Not what we want for this use case
   nodeLinker: node-modules

   // What we want for this use case
   // comment/remove it
   //nodeLinker: node-modules
   ...
}
  • Your current terminal is located in the root of your Project您当前的终端位于项目的根目录

Getting ESLint to work with VSCodeESLintVSCode一起工作

  1. First make sure your DOT files are at the top-level of the directory首先确保您的 DOT 文件位于目录的顶层
  2. Run yarn dlx @yarnpkg/sdks IDE_NAME_HERE运行yarn dlx @yarnpkg/sdks IDE_NAME_HERE
    • EX: yarn dlx @yarnpkg/sdks vscode例如: yarn dlx @yarnpkg/sdks vscode
  3. Run跑步
mkdir .vscode \
&& touch .vscode/settings.json
  1. Add the following to the newly created settings.json将以下内容添加到新创建的设置中。json
{
    // .vscode/settings.json
    "eslint.packageManager": "yarn",
    "eslint.nodePath": ".yarn/sdks",
    // If using Prettier + ESLint
    // "prettier.prettierPath": ".yarn/sdks/prettier/index.js",
}
  1. Run ESLint and then remember to turn off all the rules (Can't get any errors if the errors dont exist)运行 ESLint,然后记得关闭所有规则(如果错误不存在,则不会出现任何错误)

I am not an expert on Yarn so if anyone can chime in please correct this.我不是 Yarn 方面的专家,所以如果有人可以插话,请更正此问题。

From my understanding the PnP (Plug and Play module handler) removes itself from the conventional naming and location of: node_modules/ .根据我的理解,PnP(即插即用模块处理程序)将自己从以下常规命名和位置中删除: node_modules/

Instead what yarn does is this fancy module splitting and handling, this creates several different folders and forces us to redefine the target directory of our module dependencies (dev included) which we call in Package.json.相反,yarn 所做的是这种奇特的模块拆分和处理,这会创建几个不同的文件夹,并迫使我们重新定义我们在 Package.json 中调用的模块dependencies项(包括 dev)的目标目录。

In this case we are trying to find the target directory which holds ESlint.在这种情况下,我们试图找到包含 ESlint 的目标目录。

Great read by a very smart dev: https://psidium.github.io/lerna/monorepo/yarn/nodejs/pnp/zero-install/2021/08/23/migrating-a-monorepo-from-lerna-to-yarn-3.html非常聪明的开发者的精彩阅读: https://psidium.github.io/lerna/monorepo/yarn/nodejs/pnp/zero-install/2021/08/23/migrating-a-monorepo-from-lerna-to-纱线-3.html

Having a similar issue with a new create-react-app based on the typescript template.基于打字稿模板的新create-react-app有类似的问题。 A workaround seems to be to yarn why eslint , at which point yarn recognizes that eslint is in fact installed through react-scripts .一种解决方法似乎是yarn why eslint ,此时 yarn 认识到eslint实际上是通过react-scripts安装的。

I ended up adding it manually with yarn add "eslint@^7.11.0" (the version in react-scripts ).我最终使用yarn add "eslint@^7.11.0"react-scripts的版本)手动添加了它。 After this, yarn eslint should now be a recognized command.在此之后, yarn eslint现在应该是一个可识别的命令。 Running eslint on the App file revealed that some other dependencies are necessary (at least in my config):在 App 文件上运行eslint表明需要一些其他依赖项(至少在我的配置中):

yarn eslint --fix src\App.tsx
Oops! Something went wrong! :(

ESLint: 7.32.0

ESLint couldn't find the plugin "eslint-plugin-flowtype".

(The package "eslint-plugin-flowtype" was not found when loaded as a Node 
module from the directory "D:\my-app".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

    npm install eslint-plugin-flowtype@latest --save-dev

The plugin "eslint-plugin-flowtype" was referenced from the config file in ".eslintrc » eslint-config-react-app".

Running this a few more times shows what dependencies are missing - install them as they show up and it should eventually work.再运行几次会显示缺少哪些依赖项 - 在它们出现时安装它们,它最终应该可以工作。 Re-do the SDK step yarn dlx @yarnpkg/sdks vscode and it should be picked up by vscode.重新执行 SDK 步骤yarn dlx @yarnpkg/sdks vscode ,它应该被 vscode 拾取。 If you get an issue like "Resolve error: unable to load resolver 'node'" , try adding eslint-import-resolver-node .如果您遇到类似“解决错误:无法加载解析器‘节点’”之类的问题,请尝试添加eslint-import-resolver-node

Not perfect but a workaround for now...不完美,但目前的解决方法......

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

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