繁体   English   中英

在 React 中找不到 TypeScript 模块?

[英]TypeScript module not found in React?

我尝试通过将 .js 文件重命名为 .tsx 并通过 CL 安装它,从而在我的 create-react-app 项目中添加 typescript,如下所示:

npm install --save typescript @types/node @types/react
npm install --save typescript @types/react-dom @types/jest

但是当我重新启动开发服务器时,我收到错误“编译时出现问题:X ERROR in./src/index.tsx 7:0-24 Module not found: Error: Can't resolve './App' in 'C: \Users\user\Downloads\React\project\src' "

这是 index.tsx:

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

reportWebVitals();

我怎样才能解决这个问题?

你试过这两个步骤吗?

  1. 将 TypeScript 添加到项目中

    npm install --save typescript @types/node @types/react @types/react-dom @types/jest

  2. 添加 tsconfig.json

    npx tsc --init

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM