【问题标题】:Component files not importing in webpack react project?组件文件未在 webpack react 项目中导入?
【发布时间】:2026-01-10 10:05:01
【问题描述】:

我已经设置了一个非常简单的 React 应用程序,但我无法将任何组件导入 index.js。我的 index.js,其中包含我的主要 <App /> 类的定义,工作正常,例如有这一行:

import { IntroductionPage } from './Components/IntroductionPage.js';

从 IntroductionPage.js 导出的 IntroductionPage 组件的定义很好,我得到的是关于 IntroductionPage 在 index.js 中未定义的错误:

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of `App`.
    in App

我不确定要更改什么——我可以看到来自 IntroductionPage.js 的 console.log 输出,所以它正在运行/编译。如果我将 IntroductionPage 组件定义移动到 index.js 中,一切都会很好。不知何故,我在导入/导出步骤中丢失了它。

为什么会发生这种情况?

【问题讨论】:

  • 你能告诉我你的 IntroductionPage.js 的代码吗?您可以尝试仅使用以下内容从导入中删除括号吗?从 './Components/IntroductionPage.js' 导入 IntroductionPage;
  • 我们有一个赢家....移除固定它的括号。谢谢。
  • 您应该将该评论作为答案。
  • 当然,现在就可以了:)

标签: reactjs webpack webpack-dev-server


【解决方案1】:

我想对在 react 中起作用的导入和导出场景进行更多解释。

这是一个默认导入

// App.js
import IntroductionPage from './IntroductionPage'

仅当IntroductionPage 包含默认导出时才有效:

// IntroductionPage.js
export default 50

在这种情况下,导入时分配给它的名称并不重要:

// App.js
import IntroductionPage from './IntroductionPage'
import MySample from './IntroductionPage'
import Test from './IntroductionPage'

因为它总是会解析为IntroductionPage默认导出


这是一个名为IntroductionPage命名导入

import { IntroductionPage } from './IntroductionPage'

仅当IntroductionPage 包含一个名为IntroductionPage命名导出 时才有效:

export const IntroductionPage = 52

在这种情况下,名称很重要,因为您要通过导出名称导入特定事物

// App.js
import { IntroductionPage } from './IntroductionPage'
import { mySample } from './IntroductionPage' // Doesn't work!
import { Test} from './IntroductionPage' // Doesn't work!

要使这些工作,您将添加一个对应的命名导出IntroductionPage

// IntroductionPage.js
export const IntroductionPage = 50
export const mySample = 51
export const Test= 52

一个模块只能有一个默认导出,但命名导出的数量随你喜欢(零个、一个或多个)。您可以将它们一起导入:

// App.js
import IntroductionPage, { mySample, Test } from './IntroductionPage'

这里,我们将默认导出导入为 IntroductionPage,并将导出命名为 mySample 和 Test。

// IntroductionPage.js
export default 50
export const mySample= 51
export const Test= 52

我们也可以在导入的时候给它们分配不同的名字:

// App.js
import X, { mySample as myTest, Test as myTest2} from './IntroductionPage'

【讨论】:

    【解决方案2】:

    请尝试仅使用以下代码从导入中删除括号:

    import IntroductionPage from './Components/IntroductionPage.js';
    

    查看 MDN 上的 import docs——如果您在课堂上使用 export default,则在导入时不需要括号。

    【讨论】:

      【解决方案3】:

      你应该试试这个package.json 并安装 react 的样板。 package.json 文件在这里:

          {
        "name": "bp",
        "version": "0.1.0",
        "private": true,
        "dependencies": {
          "react": "^15.4.2",
          "react-dom": "^15.4.2"
        },
        "devDependencies": {
          "react-scripts": "0.9.5"
        },
        "scripts": {
          "start": "react-scripts start",
          "build": "react-scripts build",
          "test": "react-scripts test --env=jsdom",
          "eject": "react-scripts eject"
        }
      }
      

      然后去cmd里一一写这些命令:

      > npm install
      > npm install -g create-react-app
      > create-react-app my-app
      > 
      > cd my-app npm start
      

      【讨论】:

      • 我理解您的想法,重置我的所有配置以消除它是一些神秘的配置错误的可能性,但最终我需要一个比“重新开始”更好的解决方案。顺便欢迎使用 Stack Overflow!