ReactJS-找不到模块:错误:可以解决' app'
我只是使用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 <
/code>:
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
I just create a React project using create-react-app
and after setting up, I got this error:
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
Here is my 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")
);
I wonder what I did wrong, I want to set path for importing at the src
folder, that's why I'm using jsconfig.json
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您在项目中使用Typscript,请确保存在
tsconfig.json
文件。否则,手动或运行此命令来创建此文件:If you are using TypScript in your project, make sure the
tsconfig.json
file exists. Otherwise, create this file manually or by running this command:使用TSCONFIG文件使用
之后,将其添加到允许JSX中的typeScript中。
Create tsconfig file using
After that add this to allow jsx in typescript.
如果您是基于Typescript模板创建应用程序,而Tsconfig文件未创建,则最近出现此错误。
您可以通过以下以下方法来解决:
在index.tsx中,请参阅带有扩展名的导入(如其他帖子中建议),
如果您有ReportWebvitals的引用,您仍然可能会出现错误。还使用以下扩展名来修复此问题:
以上两个更改应解决您的问题。
但是,这种修复方法将需要您在带有扩展的每个导入中引用.tsx文件。
因此,尝试在项目的根文件夹中创建具有标准编译属性的tsconfig.json。它应该解决该问题,您可能每次都不需要带有扩展名的每个.tsx文件。
This error comes up recently if you are creating app based on typescript template and tsconfig file doesn't get created.
You can fix by following below approach:
in index.tsx, refer./App import with extension (as suggested in other post)
You may still get error if you have reference of reportWebVitals. Fix this also with the extension as below:
Above two changes should fix your issue.
But this way of fixes will need you to refer .tsx file in every imports with extension.
So, try creating tsconfig.json with standard compilerOptions attributes in root folder of project. It should fix the issue and you may not need to refer each .tsx file with extension every time.
看来您需要将
app
的导入作为相对路径。更改应该
解决问题。至于为什么需要这样做,您需要用点开始导入,以确保Node.js知道您正在尝试导入本地文件。否则,Node.js会认为您正在尝试导入模块。您可以阅读有关导入指数的更多信息在这里。
It looks like you need to make the import for
App
a relative path. Changingto
should do the trick. As to why this needs to be done, you need to start out an import with a dot to ensure that Node.JS knows you're trying to import a local file. Otherwise, Node.JS will think you're trying to import a module. You can read more about import specifiers here.
找不到模块:错误:无法解决'./app'
我遇到了同样的问题,经过长时间的研究,我意识到自己必须包括文件的后缀,我通过添加来解决我的问题
.tsx
./应用程序
实例:
具有错误的代码:<代码>从'./app'导入应用程序;
编译已修复:
来自'./app.tsx';
的导入应用程序如果解决了您的问题,请尝试一下!
Module not found: Error: Can't resolve './App'
I had faced the same issue, after a long research, what I realized myself is that you've got to include suffix of your file, I fixed my issue by adding
.tsx
behind./App
Instance:
Code having error:
import App from './App';
Compilation Fixed:
import App from './App.tsx';
Try it out if this fixes your issue!