我使用yarn@3
工作区创建了一个 monorepo。
我的根package.json
:
{
"name": "hello-yarn-workspaces",
"packageManager": "yarn@3.1.1",
"workspaces": [
"apps/*",
"packages/*"
],
"devDependencies": {
"@commitlint/cli": "^16.0.1",
"@commitlint/config-conventional": "^16.0.0",
"husky": "^7.0.4"
},
"scripts": {
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable"
}
}
package.json
在apps/ui
:_
{
"name": "ui",
"packageManager": "yarn@3.1.1",
"scripts": {
"dev": "vite"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@tailwindcss/forms": "^0.4.0",
"@types/react": "^17.0.38",
"@types/react-dom": "^17.0.11",
"@vitejs/plugin-react": "^1.1.3",
"autoprefixer": "latest",
"postcss": "latest",
"tailwindcss": "latest",
"typescript": "^4.5.4",
"vite": "^2.7.10"
}
}
我在我的apps/ui/src
文件夹中创建了一个组件,当我运行时yarn workspace ui run dev
,可以在浏览器中启动该应用程序。
但是,在 VS Code 中打开我的 monorepo 时,它无法解析import
语句中的 npm 包:
// Cannot find module 'react' or its corresponding type declarations.ts(2307)
import React, { ReactElement, useState } from 'react'
同样的情况发生vite.config.ts
在apps/ui
// Cannot find module 'vite' or its corresponding type declarations.ts(2307)
import { defineConfig } from 'vite'
// Cannot find module '@vitejs/plugin-react' or its corresponding type declarations.ts(2307)
import react from '@vitejs/plugin-react'
在 WebStorm 中打开 monorepo 时,一切正常。
可以在此处找到 repro 存储库。
更新:看起来它与PnP
机制有关。我遇到了这个问题并设置nodeLinker: node-modules
了,.yarnrc.yml
然后yarn install
修复了它。
但是,这个问题的分析器对我不起作用。
运行后我在 VS Code 中收到此错误yarn dlx @yarnpkg/sdks vscode
:
The path /Users/alexzeitler/src/hello-yarn-workspaces/.yarn/sdks/typescript/lib/tsserver.js doesn't point to a valid tsserver install. Falling back to bundled TypeScript version.
中的文件.yarn/sdks/typescript/lib
实际上不存在,但我有一个integrations.yml
文件.yarn/sdks
:
# This file is automatically generated by @yarnpkg/sdks.
# Manual changes might be lost!
integrations:
- vscode