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

运行Orval时遭遇SyntaxError: Unexpected token ':'错误求助

在Bun环境下运行Orval时遭遇SyntaxError的解决办法

问题背景

Mac系统的Bun环境中,基于Vite+TypeScript+React的项目执行Orval时,抛出如下语法错误:

Error compiling schema, function code: const schema16 = scope.schema[10];return function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;if((!(data && typeof data == "object" && !Array.isArray(data))) && (data !== null)){const err0 = {instancePath,schemaPath:"#/type",keyword:"type",params:{type: schema16.type},message:"must be object,null"};if(vErrors === null){vErrors = [err0];}else {vErrors.push(err0);}errors++;}if(data && typeof data == "object" && !Array.isArray(data)){for(const key0 in data){if(!(key0 === "keyedBy")){const err1 = {instancePath,schemaPath:"#/additionalProperties",keyword:"additionalProperties",params:{additionalProperty: key0},message:"must NOT have additional properties"};if(vErrors === null){vErrors = [err1];}else {vErrors.push(err1);}errors++;}}if(data.keyedBy !== undefined){if(typeof data.keyedBy !== "string"){const err2 = {instancePath:instancePath+"/keyedBy",schemaPath:"#/properties/keyedBy/type",keyword:"type",params:{type: "string"},message:"must be string"};if(vErrors === null){vErrors = [err2];}else {vErrors.push(err2);}errors++;}}}if(errors > 0){const emErrors0 = {"type":[]};for(const err3 of vErrors){if(((((({"str":"err3"}.keyword !== "errorMessage") && (!{"str":"err3"}.emUsed)) && ({"str":"err3"}.instancePath === instancePath)) && ({"str":"err3"}.keyword in {"str":"emErrors0"})) && ({"str":"err3"}.schemaPath.indexOf("#") === 0)) && (/^/[^/]*$/.test({"str":"err3"}.schemaPath.slice(1)))){{"str":"emErrors0"}[{"str":"err3"}.keyword].push({"str":"err3"});{"str":"err3"}.emUsed = true;}}for(const key1 in emErrors0){if({"str":"emErrors0"}[{"str":"key1"}].length){if(vErrors === null){vErrors = [{"str":"err4"}];}else {vErrors.push({"str":"err4"});}errors++;}}const emErrs0 = [];for(const err5 of vErrors){if(!{"str":"err5"}.emUsed){{"str":"emErrs0"}.push({"str":"err5"});}}vErrors = emErrs0;errors = {"str":"emErrs0"}.length;}validate14.errors = vErrors;return errors === 0;}
~/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:125
        throw e;
        ^

SyntaxError: Unexpected token ':'
    at new Function (<anonymous>)
    at Ajv.compileSchema (~/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:92:30)
    at Ajv._compileSchemaEnv (~/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:473:37)
    at Ajv.compile (~/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:160:38)
    at createRulesetFunction (~/node_modules/@stoplight/spectral-core/dist/ruleset/function.js:68:80)
    at Object.<anonymous> (~/node_modules/@stoplight/spectral-functions/dist/alphabetical.js:30:61)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)
    at Module._load (node:internal/modules/cjs/loader:1127:12)

已尝试过的无效操作:

  • 添加/省略orval.config.ts(或.js)配置文件
  • 通过bunx orval及package.json脚本两种方式执行
  • 降级Orval至7.x版本

该错误与stoplightio/spectral项目的相关问题描述一致,Orval依赖该库。

可行解决方案

1. 强制锁定兼容的Ajv版本

错误源于依赖链中Ajv版本与Bun的兼容性问题,在package.json中添加resolutions字段强制指定兼容版本:

{
  "resolutions": {
    "@stoplight/spectral-core/ajv": "8.12.0"
  }
}

执行命令重新安装依赖:

bun install

2. 切换至Node.js环境执行Orval

若上述方案无效,可临时改用Node.js运行Orval:

  • 确保本地已安装Node.js
  • 直接执行:
npx orval

或在package.json的scripts中配置:

{
  "scripts": {
    "orval": "orval"
  }
}

再运行:

npm run orval

3. 禁用Spectral校验规则

修改Orval配置文件,关闭可能触发错误的Spectral校验:
在orval.config.ts中添加:

export default {
  // 保留其他原有配置
  validate: {
    spectral: false
  }
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 17:49:56