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

本地导出SolPG项目运行client脚本遇TS1378错误求助

解决Solana Anchor项目本地运行时的顶层await TypeScript错误

你遇到的错误是因为TypeScript对顶层await有严格的配置要求,当前tsconfig.json的module和target参数不满足条件,有两种可行的解决方式:

方案一:修改tsconfig.json配置

更新你的tsconfig.json,将编译选项调整为支持顶层await的设置:

{
  "compilerOptions": {
    "types": ["mocha", "chai"],
    "typeRoots": ["./node_modules/@types"],
    "lib": ["es2017"],
    "module": "es2022",
    "target": "es2017",
    "esModuleInterop": true,
    "moduleResolution": "node"
  }
}
  • 将module设为es2022(满足错误提示中允许的模块类型)
  • 把target和lib升级到es2017及以上,确保支持异步语法
  • 添加moduleResolution: "node"避免模块解析问题

修改后重新运行anchor run client即可。

方案二:将顶层await包裹到async函数中

如果不想修改配置,可以调整client.ts代码,把使用await的逻辑放到一个异步函数里执行:

import * as anchor from "@coral-xyz/anchor";
import * as web3 from "@solana/web3.js";
import type { SolanaLamportTransfer } from "../target/types/solana_lamport_transfer";

// Configure the client to use the local cluster
anchor.setProvider(anchor.AnchorProvider.env());

const program = anchor.workspace.SolanaLamportTransfer as anchor.Program<SolanaLamportTransfer>;

async function main() {
  // Client
  console.log("My address:", program.provider.publicKey.toString());
  const balance = await program.provider.connection.getBalance(program.provider.publicKey);
  console.log(`My balance: ${balance / web3.LAMPORTS_PER_SOL} SOL`);
}

// 执行主函数并捕获错误
main().catch(err => {
  console.error(err);
  process.exit(1);
});

这种方式通过将异步逻辑封装到main函数中,避开了顶层await的限制,同时保留了原有功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 10:07:28