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

React项目运行Apollo Server报错:无法找到模块问题求助

问题分析与解决步骤

1. 修复start脚本路径错误

报错核心原因是执行的文件路径和TypeScript编译输出路径不匹配:

  • tsconfig里outDir设为dist,意味着编译后的JS文件会输出到dist目录,而非src目录
  • 你的start脚本却试图运行./src/index.js,但src下只有index.ts,不存在这个JS文件

修改package.json的scripts配置:

"start": "npm run compile && node ./dist/index.js"

2. 调整TypeScript模块编译配置

你的tsconfig中module设为ESNext,但Node.js默认使用CommonJS模块系统,直接运行会出现语法兼容问题,可二选一调整:

选项A:改用CommonJS模块(推荐,无需额外修改package.json)

修改tsconfig.json的compilerOptions:

"module": "CommonJS",

选项B:启用ES模块支持

  • 在package.json根节点添加"type": "module"
  • 同时更新tsconfig的compilerOptions:
"module": "ESNext",
"moduleResolution": "NodeNext",

3. 补充tsconfig的include配置(可选但建议)

如果tsconfig未指定include,TypeScript可能不会编译src下的所有文件,建议添加:

"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]

验证步骤

  1. 执行npm run compile,检查dist目录是否生成index.js文件
  2. 执行npm start,确认Apollo Server正常启动

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 21:47:19