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
- Create a new Next.js project with TypeScript:
npx create-next-app@latest my-app --typescript
cd my-app
- Add Prisma:
npm install prisma @prisma/client
npx prisma init
- 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();
});
- Update
package.json
:
{
"prisma": {
"seed": "ts-node --compiler-options {"module":"CommonJS"} prisma/seed.ts"
}
}
- 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.
- Install it:
npm install --save-dev tsx
- Update
package.json
:
{
"prisma": {
"seed": "tsx prisma/seed.ts"
}
}
- 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.