typescript VSCode TS2307找不到模块

weylhg0b  于 2022-11-26  发布在  TypeScript
关注(0)|答案(3)|浏览(303)

在VSCode中打开Typescript项目会导致错误“ts2307找不到模块'react'或其相应的类型声明”。但Webstorm工作正常。我从CRA创建了repo,即typescript模板。节点通过nvm安装。我在这里使用yarn 3.2.3,并且我没有弹出CRA。
package.json

{
  "name": "frontend-ts",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^13.0.0",
    "@testing-library/user-event": "^13.2.1",
    "@types/jest": "^27.0.1",
    "@types/node": "^16.7.13",
    "@types/react": "^18.0.0",
    "@types/react-dom": "^18.0.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.4.2",
    "web-vitals": "^2.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

6vl6ewon

6vl6ewon1#

您需要安装npm i --save-dev @types/react

cygmwpex

cygmwpex2#

我注意到我错过了Yarn工作空间的概念。当我实现this时,它就被解决了!工作空间允许node_modules存在于某个地方并满足VSCode。

dly7yett

dly7yett3#

yarn 3新版本,需要额外的配置vscode修复这个编辑器的问题,请参考以下三个链接:

==固定步骤==================
1.创建Yarn工作空间
1.Yarn(在工作区)
1.Yarndlx @Yarn Package /sdks vscode

  1. vscode选择工作区类型脚本https://yarnpkg.com/getting-started/editor-sdks#vscode
  2. vscode将建议使用安装zipfs扩展,insall它(也许使用可以自己安装zipfs)

相关问题