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

Node.js使用chalk-animation等工具遇TypeScript报错求助

解决TypeScript报错方案

错误1:Module "@types/chalk-animation/index" has no default export

将默认导入改为命名导入,二选一即可:

  • 导入整个模块作为命名空间:
    import * as chalkAnimation from "chalk-animation";
    
  • 按需导入具体动画方法(比如你用到的rainbow/glitch等):
    import { rainbow } from "chalk-animation";
    // 使用时直接调用rainbow(),而非chalkAnimation.rainbow()
    

错误2:Top-level 'await' expressions are only allowed...

两种解决方式可选:

方法1:修改tsconfig.json配置

在项目根目录的tsconfig.json中,调整compilerOptions内的对应字段:

{
  "compilerOptions": {
    "module": "node16", // 也可设为es2022、esnext、nodenext、system
    "target": "es2017", // 或更高版本如es2020、esnext
    // 保留其他原有配置
  }
}

方法2:用异步自执行函数包裹代码

若不想修改配置,把包含await的代码放到异步立即执行函数中:

(async () => {
  await welcome();
  // 其他需要await的代码也统一放这里
})();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 02:15:41