How to Fix "Unknown file extension .ts" Error When Running Prisma Seed

Published: June 9, 2025

Next.jsPrismaTypeScript

When running Prisma’s seed command in a Next.js project, you might see this:

TypeError: Unknown file extension ".ts" for prisma/seed.ts at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:219:9)
Error: Command failed with exit code 1: ts-node --compiler-options {"module":"CommonJS"} prisma/seed.ts

Why It Happens

This happens because:

  • Next.js projects usually use ESNext modules in tsconfig.json ("module": "ESNext").
  • Prisma’s default seed script expects CommonJS when using ts-node.
  • The mismatch causes Node.js to throw the .ts extension error.

Quick Reproduction

  1. Create a new Next.js project with TypeScript:
npx create-next-app@latest my-app --typescript
cd my-app
  1. Add Prisma:
npm install prisma @prisma/client
npx prisma init
  1. Create a simple seed file (prisma/seed.ts):
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
	console.log('Seeding database...');
	// seed logic
}

main()
	.catch((e) => {
		console.error(e);
		process.exit(1);
	})
	.finally(async () => {
		await prisma.$disconnect();
	});
  1. Update package.json:
{
	"prisma": {
		"seed": "ts-node --compiler-options {"module":"CommonJS"} prisma/seed.ts"
	}
}
  1. Run:
npx prisma db seed

You’ll get the error.

The Simple Fix: Use tsx

tsx is a modern alternative to ts-node that handles ESNext modules out of the box.

  1. Install it:
npm install --save-dev tsx
  1. Update package.json:
{
	"prisma": {
		"seed": "tsx prisma/seed.ts"
	}
}
  1. Run again:
npx prisma db seed

This should work as expected.


Let's connect

Always interested in good conversations about technology, football, or interesting projects.