I have a yarn workspace set up with folders public
and server
. I am using TypeScript in VS Code. On any module that doesn't have types pre-installed with the package, I get the error in VS Code:
我有一个纱线工作区设置了文件夹公共和服务器。我正在使用VS代码中的打字脚本。在没有随程序包预安装类型的任何模块上,我在VS代码中得到错误:
Cannot find module 'x'
However, the IntelliSense will show x
as a suggestion. I have both x
and @types/x
and it seems like it's no difference if I install @types/x
. Thanks
但是,IntelliSense将显示x作为建议。我既有x又有@type/x,如果我安装@type/x似乎没有什么区别。谢谢
Windows 1909 (Latest)
Visual Studio Code 1.43 (Latest)
TypeScript ^3.8.3 (Latest)
Yarn 2.0.0-rc.30 (Latest)
Also, I tried to find type roots for yarn, but didn't. Could someone please tell me where the type root directry is.
另外,我试着为纱线找类型根,但没有。有人能告诉我类型根目录在哪里吗?
更多回答
Have a look at the instructions here: https://yarnpkg.com/getting-started/editor-sdks
请看这里的说明:https://yarnpkg.com/getting-started/editor-sdks
Smart IDEs (such as VSCode or IntelliJ) require special configuration for TypeScript to work. This page intends to be a collection of settings for each editor we've worked with - please contribute to this list!
For example when using a "vscode" IDE execute:
例如,当使用“vscode”IDE时,执行:
yarn dlx @yarnpkg/pnpify --sdk vscode
The above command will wire up your vscode project to rely on yarn, i.e. it adds the sdks
folder within your .yarn
config and also tells your IDE to use it, by adding the following to your settings.json
:
上面的命令将连接您的vscode项目以依赖YAR,也就是说,它会将SDK文件夹添加到您的.spar配置中,并通过将以下内容添加到settings.json来告诉您的IDE使用它:
"typescript.tsdk": ".yarn/sdks/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
Hint: Yarn caches its packages in form of zip archives. So in order to be also able to inspect your node_modules
imports managed by yarn, you can install the ZipFS extension
提示:纱线以压缩存档的形式缓存其包裹。因此,为了也能够检查由YAR管理的node_MODULES导入,您可以安装ZipFS扩展
This extension adds support into VSCode to read files directly from zip archives. It's maintained as part of the Yarn toolchain. Using this extension together with the Yarn SDK will allow you to seamlessly open & edit files from your cache.
From the docs:
从docs:
To support features like go-to-definition a plugin like ZipFS is needed.
Run the following command, which will generate a new directory called .yarn/sdks
:
yarn dlx @yarnpkg/sdks vscode
For safety reason VSCode requires you to explicitly activate the custom TS settings:
Press ctrl+shift+p in a TypeScript file
Choose "Select TypeScript Version"
Pick "Use Workspace Version"
Similar issue here. This error is coming from the linter; I think it's checking inside the package lock file (which does not exist as yarn is using yarn.lock
).
这里也有类似的问题。这个错误来自林特;我认为它是在检查包锁文件(该文件不存在,因为纱线正在使用ya n.lock)。
Please check these in your VSCode user settings.json
:
请在您的VSCode用户设置中检查这些设置。json:
"eslint.packageManager": "yarn",
"prettier.packageManager": "yarn",
"npm.packageManager": "yarn"
These settings have worked for me. YMMV.
这些设置对我很有效。YMMV。
EDIT: changed punctuation
编辑:更改的标点符号
It took almost half an hour to solve this. The steps are:
花了将近半个小时才解决了这个问题。具体步骤如下:
- Install ZipFS extension for VSCode because you might need to see some definitions.
- Run
yarn dlx @yarnpkg/sdks vscode
to install VSCode support for Yarn.
- Open command palette with CTRL+SHIFT+P and choose "Select TypeScript version".
- Choose "Workspace Version" that starts with
.yarn
.
- On terminal, do
yarn config set nodeLinker node-modules
and then yarn
.
更多回答
Thanks for posting this documentation link. Unfortunately, I'm on yarn 1 and this requires yarn 2.
感谢您张贴此文档链接。不幸的是,我使用的是纱线1,而这需要纱线2。
For yarn 3, the command is yarn dlx @yarnpkg/sdks vscode
. Also the .vscode folder must be in the VSCode workspace root (move it and fix the paths inside if necessary).
对于纱线3,命令是纱线dlx@ya npkg/sdks vscode。此外,.vscode文件夹必须位于VSCode工作区根目录中(如有必要,请移动它并修复其中的路径)。
I tried the accepted answer first (set up a brand new project with yarn 3), but steps 4 and 5 are what fixed my problem. Thank you!
我首先尝试了公认的答案(用纱线3建立了一个全新的项目),但步骤4和5解决了我的问题。谢谢!
Didn't work for me, plus the first two says it's unknown
对我不起作用,加上前两个人说这是未知的
eslint and prettier are resolved automatically in the latest version of VSCode (1.80.0)
在最新版本的VSCode(1.80.0)中会自动解析eslint和PRETER
我是一名优秀的程序员,十分优秀!