TypeScript错误:找不到模块';反应';

TypeScript错误:找不到模块';反应';,typescript,tsc,Typescript,Tsc,我已经试着编译我的项目好几个小时了,我在互联网上到处搜索,但我仍然不明白为什么它不起作用 我刚刚运行了npx tsc,但出现了以下错误: $npx tsc src/index.ts:2:24-错误TS2307:找不到模块“React”。 2从“React”导入*作为React; ~~~~~~~ 发现1个错误。 tsconfig.json: { "compilerOptions": { "moduleResolution": "node", "target": "es6",

我已经试着编译我的项目好几个小时了,我在互联网上到处搜索,但我仍然不明白为什么它不起作用

我刚刚运行了npx tsc,但出现了以下错误:

$npx tsc
src/index.ts:2:24-错误TS2307:找不到模块“React”。
2从“React”导入*作为React;
~~~~~~~
发现1个错误。
tsconfig.json:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es6",
    "module": "commonjs",
    "esModuleInterop": true,
    "lib": [
      "es2015",
      "es2016",
      "es2017",
      "dom"
    ],
    "strict": true,
    "sourceMap": true,
    "declaration": true,
    "declarationDir": "dist/types",
    "outDir": "dist/lib",
    "typeRoots": [
      "node_modules/@types"
    ],
    "jsx": "react",
    "noResolve": false,
    "allowJs": false,
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules",
    "dist",
    "playground"
  ]
}
src/index.ts:

从“lodash”导入lodash;
从“React”导入*作为React;
packages.json:

{
  "files": [
    "dist"
  ],
  "devDependencies": {
    "@types/lodash": "^4.14.144",
    "@types/node": "^12.11.7",
    "@types/react": "^16.9.11",
    "lodash": "^4.17.15",
    "react": "^16.11.0",
    "ts-node": "^8.4.1",
    "typescript": "^3.6.4"
  }
}

提供了什么?

您可能应该将
react
安装为依赖项,而不是
devDependency

另外,将react导入文件的第一行。

除了通过运行

npm i react
您可能希望导入默认的导出模块

import React from 'react';
tsconfig.json
上,您可以将
allowSyntheticDefaultImports
设置为
true

"allowSyntheticDefaultImports": true
此外,您还可以阅读有关集成的更多信息