找不到模块“ react-dom”的声明文件

时间:2020-04-27 20:46:15

标签: reactjs typescript

我已经使用Typescript安装了React项目:

npx create-react-app my-app --template typescript

使用npm run start运行应用程序后

我收到此错误:

TypeScript error in C:/Users/neman/Desktop/projects/redux-saga-ts-boilerplate/src/index.tsx(2,22):
Could not find a declaration file for module 'react-dom'. 'C:/Users/neman/Desktop/projects/redux-saga-ts-boilerplate/node_modules/react-dom/index.js' implicitly has an 'any' type.
  If the 'react-dom' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-dom`  TS7016

    1 | import React from 'react';
  > 2 | import ReactDOM from 'react-dom';
      |                      ^
    3 | import './index.css';
    4 | import App from './App';
    5 | import * as serviceWorker from './serviceWorker';

这是我的package.json

{
  "name": "redux-saga-ts-boilerplate",
  "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",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-redux": "^7.1.7",
    "@types/redux-actions": "^2.6.1",
    "@types/redux-logger": "^3.0.7",
    "axios": "^0.19.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-redux": "^7.2.0",
    "react-scripts": "3.4.1",
    "redux": "^4.0.5",
    "redux-actions": "^2.6.5",
    "redux-logger": "^3.0.6",
    "redux-saga": "^1.1.3",
    "typescript": "~3.7.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "reinstall": "rm -rf ./node_modules && npm install",
    "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"
    ]
  }
}

如您所见,我确实安装了"@types/react-dom"

这是我的tsconfig.json文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": ["src"]
}

那么,这是什么问题?

2 个答案:

答案 0 :(得分:1)

我有同样的问题。在遇到answer之后遇到一个稍微不同的问题,然后解决了这个问题。试试这个:

npm install --save @types/react-dom

希望这对您有用!

答案 1 :(得分:0)

这是一个很常见的问题,如果你的项目仍然没有解决,但这不是一个好的工作方式,研究每一件事并解决这个问题,然后必须去。

npm install --save @types/react-dom

此命令有效。