You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js出现Element type is invalid错误,该如何排查解决?

解决Next.js中Element type invalid错误

错误原因

这个错误是客户端组件与服务器组件混用导致的:在Next.js App Router中,组件默认是服务器组件,但Ant Design的UI组件只能在客户端组件中运行。你的LoginForm组件没有添加"use client"指令,被当作服务器组件处理后,内部导入的Ant Design组件无法在服务器环境初始化,最终返回undefined,触发该错误。

解决方案

在LoginForm组件的最顶部添加"use client"指令,修改后的代码如下:

"use client";
import {
  Form,
  Input,
  UserOutlined,
  LockOutlined,
  Checkbox,
  Button,
} from "antd";

const LoginForm = () => (
  <div>
    <Form name="basic" labelCol={{ span: 8 }}>
      <Form.Item
        name="username"
        rules={[{ required: true, message: "Please input your Username!" }]}
      >
        <Input
          prefix={<UserOutlined className="site-form-item-icon" />}
          placeholder="Username"
        />
      </Form.Item>
      <Form.Item
        name="password"
        rules={[{ required: true, message: "Please input your Password!" }]}
      >
        <Input
          prefix={<LockOutlined className="site-form-item-icon" />}
          type="password"
          placeholder="Password"
        />
      </Form.Item>
      <Form.Item>
        <Form.Item name="remember" valuePropName="checked" noStyle>
          <Checkbox>Remember me</Checkbox>
        </Form.Item>
        <a className="login-form-forgot" href="">
          Forgot password
        </a>
      </Form.Item>
      <Form.Item>
        <Button type="primary" htmlType="submit" className="login-form-button">
          Log in
        </Button>
        Or <a href="">register now!</a>
      </Form.Item>
    </Form>
  </div>
);

export default LoginForm;

额外排查方向

如果上述方法无效,可检查以下内容:

  • 确认Ant Design版本为v5.x,v4版本在Next.js App Router下存在兼容问题,可通过npm list antd查看版本
  • 核对LoginForm的导入路径../components/LoginForm是否正确,注意Linux/macOS环境区分文件名大小写
  • 删除.next文件夹并重启开发服务器,清除缓存异常

内容的提问来源于stack exchange,提问作者Maksym Yuzva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 19:24:52