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

如何正确运行React-Relay的relay-compiler解决npm start启动报错问题

问题原因

你收到missing script: relay-compiler报错的核心原因是package.json的scripts配置逻辑错误:

  • npm run 仅能执行scripts字段中自定义的脚本命令,而relay-compiler是你安装在项目node_modules中的可执行依赖,不属于自定义脚本范畴,无法通过npm run直接调用
  • 脚本末尾的$@是类Unix系统的参数传递语法,Windows系统下无法识别,也可能引发额外报错
修复方案

直接修改package.json中的relay脚本配置即可,修改后的完整scripts片段如下:

{
  ...
  "scripts": {
    ...
    "start": "npm run relay && react-scripts start",
    "build": "npm run relay && react-scripts build",
    "relay": "relay-compiler --schema schema.graphql --src ./src/ --watchman false"
    ...
  },
  ...
}

说明:npm执行自定义脚本时,会自动将node_modules/.bin目录加入临时环境变量,直接写relay-compiler就能调用本地安装的依赖。如果调用失败,也可以将上述relay脚本内容替换为npx relay-compiler --schema schema.graphql --src ./src/ --watchman false,通过npx主动寻址本地依赖。

验证步骤
  1. 确认项目根目录存在schema.graphql schema文件,避免后续执行报错
  2. 先执行npm run relay测试编译逻辑是否正常
  3. 再执行npm start启动项目即可

内容的提问来源于stack exchange,提问作者Chong Lip Phang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 17:36:01