Capacitor CLI读取配置文件时出现ReferenceError: await未定义问题
capacitor.config.ts时的"ReferenceError: await is not defined"错误 问题场景
使用Capacitor CLI读取capacitor.config.ts文件时触发ReferenceError: await is not defined错误,此前在Vite中遇到过相同问题但无法复现。
报错的capacitor.config.ts代码
import type {AppConfig} from '../../utils/app-config'; import {resolveAllConfigs} from '../../utils/app-config'; import type {CapacitorConfig} from '@capacitor/cli'; const appConfig: AppConfig = (await resolveAllConfigs({})); export default ({} as CapacitorConfig);
错误信息
[error] Parsing capacitor.config.ts failed.
ReferenceError: await is not defined
at Object.
(/Users/oliver/Desktop/temp/app-mobile/mobile/packages/app/capacitor.config.ts:4:19)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at require.extensions..ts
(/Users/oliver/Desktop/temp/app-mobile/mobile/node_modules/.pnpm/@capacitor+cli@5.4.1/node_modules/@capacitor/cli/dist/util/node.js:34:72)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:121:18)
at requireTS
(/Users/oliver/Desktop/temp/app-mobile/mobile/node_modules/.pnpm/@capacitor+cli@5.4.1/node_modules/@capacitor/cli/dist/util/node.js:36:15)
at loadExtConfigTS
(/Users/oliver/Desktop/temp/app-mobile/mobile/node_modules/.pnpm/@capacitor+cli@5.4.1/node_modules/@capacitor/cli/dist/config.js:89:54)
at loadExtConfig
(/Users/oliver/Desktop/temp/app-mobile/mobile/node_modules/.pnpm/@capacitor+cli@5.4.1/node_modules/@capacitor/cli/dist/config.js:123:16)
相关工具代码(app-config.ts)
import type {AppConfig, AppConfigEnv} from './app-config'; import * as cc from 'cosmiconfig'; import {TypeScriptLoader} from 'cosmiconfig-typescript-loader'; import _ from 'lodash'; const COSMICONFIG_LOADERS: cc.Loaders = {'.ts': TypeScriptLoader()}; const resolveConfig = (async(moduleName: string, env: AppConfigEnv): Promise<AppConfig> => { const explorer: cc.PublicExplorer = cc.cosmiconfig(moduleName, {loaders: COSMICONFIG_LOADERS}); const result: (cc.CosmiconfigResult | null) = (await explorer.search()); if(!result) { throw (new Error(`Could not find a valid ${moduleName} configuration.`)); } if((typeof result.config === 'object')) { // Object or Promise. return (await result.config); } else if((typeof result.config === 'function')) { // Function. return (await result.config(env)); } throw (new Error(`Invalid ${moduleName} configuration.`)); }); const resolveAllConfigs = (async(env: AppConfigEnv): Promise<AppConfig> => { const isDevelopment: boolean = (env.mode === 'development'); return _.merge( (await resolveConfig('app', env)), ...(isDevelopment ? [ (await resolveConfig('app--local-default', env)), (await resolveConfig('app--local', env)) ] : []) ); }); export { resolveConfig, resolveAllConfigs };
问题原因
await关键字仅能在async函数内部使用,你在capacitor.config.ts的模块顶层直接调用await resolveAllConfigs({}),而Capacitor CLI依赖的Node.js CommonJS模块系统默认不支持顶层await,加载逻辑也未兼容该特性,因此触发错误。
解决方法
方案1:用立即执行异步函数(IIFE)包裹逻辑
将顶层await放入IIFE中执行,避免直接在模块顶层使用:
import type {AppConfig} from '../../utils/app-config'; import {resolveAllConfigs} from '../../utils/app-config'; import type {CapacitorConfig} from '@capacitor/cli'; let appConfig: AppConfig; (async () => { appConfig = await resolveAllConfigs({}); // 若需要基于appConfig生成Capacitor配置,可在此处处理 })(); export default ({} as CapacitorConfig);
方案2:导出异步函数(推荐,适配Capacitor 5+)
Capacitor 5及以上版本支持导出返回CapacitorConfig的异步函数,可直接在函数内使用await:
import type {AppConfig} from '../../utils/app-config'; import {resolveAllConfigs} from '../../utils/app-config'; import type {CapacitorConfig} from '@capacitor/cli'; export default async (): Promise<CapacitorConfig> => { const appConfig: AppConfig = await resolveAllConfigs({}); // 基于appConfig生成你的Capacitor配置 return { appId: appConfig.appId, appName: appConfig.appName, // 其他配置项 } as CapacitorConfig; };
额外说明
即使Node.js v14.8+支持顶层await,但Capacitor CLI的配置加载逻辑并未适配该特性,因此上述两种方案是更稳妥的解决方式。
内容的提问来源于stack exchange,提问作者Oliver

