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

为何graphql-upload不支持ES模块导入?求解决方案

解决graphql-upload在ES模块("type": "module")下导入失效的问题

报错原因

当项目设置"type": "module"后,Node.js会采用ES模块的解析规则处理导入请求。旧版本的graphql-upload未在package.json中配置exports字段,导致Node.js无法识别其ES模块入口,从而抛出Error [ERR_PACKAGE_PATH_NOT_EXPORTED]。

解决方案

  • 升级到支持ES模块的graphql-upload版本
    这是最优解,新版本的graphql-upload已经适配了ES模块规范。执行以下命令升级:

    npm install graphql-upload@latest
    

    升级后直接使用ES模块导入即可:

    import { GraphQLUpload } from 'graphql-upload';
    
  • 动态导入(临时兼容方案)
    如果暂时无法升级版本,可以使用Node.js的动态导入语法绕开解析限制:

    // 在异步函数中使用
    const { GraphQLUpload } = await import('graphql-upload');
    
  • 手动添加exports字段(不推荐,仅临时应急)
    找到项目中node_modules/graphql-upload/package.json,添加exports配置:

    "exports": {
      ".": {
        "import": "./index.mjs",
        "require": "./index.js"
      }
    }
    

    注意:该修改会在重新安装依赖后被覆盖,仅作为临时应急手段。

  • 单个文件切换为CommonJS
    将需要使用graphql-upload的文件后缀改为.cjs,Node.js会自动将其识别为CommonJS模块,此时可以正常使用require导入:

    // 文件名示例:upload-handler.cjs
    const { GraphQLUpload } = require('graphql-upload');
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 02:20:59