纱线:找不到模块

yarn: cannot find module

我有一个 yarn 工作区设置了文件夹 publicserver。我在 VS Code 中使用 TypeScript。在任何没有随包预安装类型的模块上,我在 VS Code 中收到错误:

Cannot find module 'x'

但是,IntelliSense 将显示 x 作为建议。 x@types/x 我都有,安装 @types/x 好像没什么区别。谢谢

Windows 1909 (Latest)
Visual Studio Code 1.43 (Latest)
TypeScript ^3.8.3 (Latest)
Yarn 2.0.0-rc.30 (Latest)

此外,我试图找到 yarn 的类型根,但没有找到。谁能告诉我类型根目录在哪里。

这里有类似的问题。这个错误来自 linter;我认为它正在检查包锁定文件(它不存在,因为 yarn 使用 yarn.lock)。

请在您的 VSCode 用户 settings.json 中检查这些:

"eslint.packageManager": "yarn",
"prettier.packageManager": "yarn",
"npm.packageManager": "yarn"

这些设置对我有用。 YMMV。

编辑:更改标点符号

查看此处的说明: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!

例如,当使用“vscode”时 IDE 执行:

yarn dlx @yarnpkg/pnpify --sdk vscode

以上命令将连接您的 vscode 项目以依赖 yarn,即它会在您的 .yarn 配置中添加 sdks 文件夹并告诉您的 IDE要使用它,请将以下内容添加到您的 settings.json:

"typescript.tsdk": ".yarn/sdks/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true

提示:Yarn 以 zip 存档的形式缓存它的包。因此,为了也能够检查由 yarn 管理的 node_modules 导入,您可以安装 ZipFS extension

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.

来自docs

要支持像转到定义这样的功能,需要像 ZipFS 这样的插件。

  1. 运行下面的命令,会生成一个名为.yarn/sdks的新目录:

    yarn dlx @yarnpkg/sdks vscode
    
  2. 出于安全原因VSCode 要求您明确激活自定义 TS 设置:

  3. 在 TypeScript 文件中按 ctrl+shift+p

  4. 选择“Select TypeScript 版本”

  5. 选择“使用工作区版本”