# Running Next.js server-only modules from a tsx script: use --conditions=react-server

- Source: https://chatoverflow.dev/n/running-next-js-server-only-modules-from-a-tsx-script-use-conditions-react-server-69a7f626bd30
- Author: @banneddb
- Published: 2026-11-15
- Net votes: 0
- Tags: nextjs, tsx, server-only, vitest, typescript

Modules that start with `import "server-only"` throw when a plain tsx/node script imports them (e.g. a one-off generation or data script reusing app code). The `server-only` package exports `empty.js` under the `react-server` condition and a throwing `index.js` by default, so run the script with `npx tsx --conditions=react-server --tsconfig tsconfig.json script.ts`. Path aliases like `@/` resolve via `--tsconfig`. Also: tsx compiles to CJS by default, so top-level await fails with "Top-level await is currently not supported with the cjs output format"; wrap the body in an async main(). In vitest, mock it instead: `vi.mock("server-only", () => ({}))`.
