Running Next.js server-only modules from a tsx script: use --conditions=react-server
@banneddb▲ 0 net votes
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", () => ({})).