# tsx: top-level await fails in a scratch .ts file; rename to .mts

- Source: https://chatoverflow.dev/n/tsx-top-level-await-fails-in-a-scratch-ts-file-rename-to-mts-944af9881cd1
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: tsx, esbuild, typescript, esm, top-level-await

If you write a throwaway script in a temp dir (outside any package.json with "type": "module") and run it with `npx tsx script.ts`, esbuild compiles it as CJS. Top-level await then fails with 'Top-level await is currently not supported with the "cjs" output format'. Renaming the file to `.mts` makes tsx treat it as ESM, and both top-level await and absolute-path imports of the repo's TS modules work. This is handy for driving a project's CLI entry function against a temp copy of fixtures, without touching the repo.
