reactjs 在JSConfig.json文件中看到未知的typescript错误

ttvkxqim  于 2022-12-03  发布在  React
关注(0)|答案(3)|浏览(159)

我在我的项目中添加了一个jsconfig.json,但由于某种原因,我得到了一个typescript错误,指出找不到dotenv/types。我一辈子都搞不清楚为什么会得到这个错误。我甚至尝试在我的package.json中添加dotenv,但它没有解决我的问题。
这是使用create-react-app创建的标准项目,我添加了jsconfig.json以允许绝对导入

// jsconfig.json    
{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

例外情况:
找不到文件'/用户/路径/目标/文件/节点模块/dotenv/类型'
其他可能有用的文件:

软件包.json

{
  "name": "quantous-spa",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.20.0",
    "babel-plugin-macros": "^2.8.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-feather": "^2.0.8",
    "react-loading-skeleton": "^2.1.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.3",
    "styled-components": "^5.1.1",
    "typeface-muli": "^1.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "eslint-config-google": "^0.14.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-plugin-prettier": "^3.1.4",
    "eslint-plugin-react": "^7.20.6",
    "prettier": "^2.0.5"
  },
  "resolutions": {
    "styled-components": "^5"
  }
}

.eslintrc

{
  "env": {
    "browser": true,
    "es2020": true
  },
  "extends": [
    "plugin:react/recommended",
    "google",
    "plugin:prettier/recommended"
  ],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true,
      "js": true
    },
    "ecmaVersion": 11,
    "sourceType": "module"
  },
  "plugins": ["react"],
  "rules": {
    "require-jsdoc": "off"
  },
  "settings": {
    "import/resolver": {
      "node": {
        "paths": ["node_modules", "src/"]
      }
    }
  }
}

osh3o9ms

osh3o9ms1#

如果您使用的是Visual Studio Code,只需在设置中禁用“Typescript validation”即可。
错误应消失。

tag5nh1u

tag5nh1u2#

@dioslibre的答案很好用,但我不想禁用Typescript验证。一个替代方案是在settings.json中添加以下代码片段,@Anees Hameed在回答类似问题时解释了这一点:

"json.schemas": [
      { 
        "$schema": "https://json.schemastore.org/jsconfig" 
      }
    ]
wlzqhblo

wlzqhblo3#

这个错误告诉你它找不到dotenv类型。运行npm install dotenv来安装类型。

相关问题