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
相关产品推荐
相关产品推荐

