ReactJS-找不到模块:错误:可以解决' app'

发布于 2025-02-13 19:01:00 字数 1159 浏览 0 评论 0原文

我只是使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦初醒的夏天 2025-02-20 19:01:00

如果您在项目中使用Typscript,请确保存在tsconfig.json文件。否则,手动或运行此命令来创建此文件:

npx tsc --init

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:

npx tsc --init
我要还你自由 2025-02-20 19:01:00

使用TSCONFIG文件使用

NPX TSC -Init

之后,将其添加到允许JSX中的typeScript中。

{
  "compilerOptions": {
    "jsx": "react",
     .....
}

Create tsconfig file using

npx tsc --init

After that add this to allow jsx in typescript.

{
  "compilerOptions": {
    "jsx": "react",
     .....
}

如果您是基于Typescript模板创建应用程序,而Tsconfig文件未创建,则最近出现此错误。
您可以通过以下以下方法来解决:
在index.tsx中,请参阅带有扩展名的导入(如其他帖子中建议),

import App from './App.tsx';

如果您有ReportWebvitals的引用,您仍然可能会出现错误。还使用以下扩展名来修复此问题:

import reportWebVitals from './reportWebVitals.ts';

以上两个更改应解决您的问题。
但是,这种修复方法将需要您在带有扩展的每个导入中引用.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)

import App from './App.tsx';

You may still get error if you have reference of reportWebVitals. Fix this also with the extension as below:

import reportWebVitals from './reportWebVitals.ts';

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.

Bonjour°[大白 2025-02-20 19:01:00

看来您需要将app的导入作为相对路径。更改

import App from "App";

应该

import App from "./App";

解决问题。至于为什么需要这样做,您需要用点开始导入,以确保Node.js知道您正在尝试导入本地文件。否则,Node.js会认为您正在尝试导入模块。您可以阅读有关导入指数的更多信息在这里

It looks like you need to make the import for App a relative path. Changing

import App from "App";

to

import App from "./App";

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.

暖阳 2025-02-20 19:01:00

找不到模块:错误:无法解决'./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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文