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

Next.js 13中使用URLPattern出现webpack构造函数错误求助

解决Next.js 13中URLPattern运行时构造函数错误

问题原因

你从next/server导入的URLPattern仅存在TypeScript类型定义,但运行时next/server并未实际导出这个类,因此触发构造函数不存在的错误。

解决方案

  • 改用Node.js原生URLPattern(推荐方案)
    Node.js 18及以上版本已原生支持URLPattern,直接使用全局对象或从node:url导入即可:

    // 方式1:直接调用全局实例
    const url = new URLPattern();
    
    // 方式2:从node:url明确导入(代码更规范)
    import { URLPattern } from 'node:url';
    const url = new URLPattern();
    
  • 升级Next.js版本
    若坚持使用next/server的导出,需确保你的Next.js版本为13.4及以上(部分旧版本未在next/server中包含URLPattern),执行升级命令:

    npm install next@latest
    
  • 清理Next.js缓存
    缓存可能导致旧代码残留,删除.next文件夹后重启开发服务器:

    rm -rf .next
    npm run dev
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 23:30:27