我刚刚使用
create-react-app
创建了一个 React 项目,设置后,我得到了这个错误:
Failed to compile.
Module not found: Error: Can't resolve 'App' in 'C:\Development_Projects\international-school-fe\src'
ERROR in ./src/index.tsx 4:0-22
Module not found: Error: Can't resolve 'App' in 'C:\Development_Projects\international-school-fe\src'
webpack compiled with 1 error
这是我的
jsconfig.json
:
{
"compilerOptions": {
"baseUrl": "src",
"checkJs": true,
"jsx": "react"
},
"include": ["src"]
}
index.tsx
:
import App from "App";
import "common/styles/spacing/margin/index.css";
import "common/styles/spacing/padding/index.css";
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
我想知道我做错了什么,我想在
src
文件夹中设置导入路径,这就是我使用 jsconfig.json 的原因
如果您在项目中使用 TypScript,请确保
tsconfig.json
文件存在。否则,手动或通过运行以下命令创建此文件:
npx tsc --init
看起来您需要将
App
的导入设为相对路径。改变
import App from "App";
到
import App from "./App";
应该可以解决问题。至于为什么需要这样做,您需要以点开始导入,以确保 Node.JS 知道您正在尝试导入本地文件。否则,Node.JS 会认为您正在尝试导入模块。您可以在此处阅读有关导入说明符的更多信息。
出现类似 Module is not found in src..../ 的错误。我使用该应答命令: npx tsc --init 我的应用程序可以正常工作。感谢 U Tayyab Chaudhary 的回答