prisma: prisma db seed doesn't correctly support `"type": "module"`

Problem

When you enable module resolution with "type": "module" on your projet the prisma db seed command fail due to ts-node resolution.

➜ yarn prisma db seed --preview-feature
yarn run v1.22.10
$ /path-to-my-project/node_modules/.bin/prisma db seed --preview-feature
Environment variables loaded from prisma/.env
Prisma schema loaded from prisma/schema.prisma
Running ts-node "prisma/seed.ts" ...
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /path-to-my-project/play-prisma-sqlite/prisma/seed.ts
    at new NodeError (node:internal/errors:329:5)
    at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
    at Loader.getFormat (node:internal/modules/esm/loader:104:42)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:31)
    at Loader.import (node:internal/modules/esm/loader:176:17)
    at Object.loadESM (node:internal/process/esm_loader:68:5)
Error: Command failed with exit code 1: ts-node "prisma/seed.ts"
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Suggested solution

When "type": "module" is detected: instead of running ts-node "prisma/seed.ts" you can use the node loader node --loader ts-node/esm "prisma/seed.ts"

Alternatives

N/A

Additional context

Project example https://github.com/ghoullier/esm-prisma-seed

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 21 (4 by maintainers)

Most upvoted comments

TSX worked for me in sveltekit

yarn add -D tsx

“prisma”: { “seed”: “tsx prisma/seed.ts” }

Had the same problem, but the ts-node esm flag fixed it for me: ts-node --esm prisma/seed.ts

This is temp case, you can run seed, but --preview-feature not work:

If your seed file in at your project prisma/seed.ts.

1 - Install esbuild in your project

npm i esbuild --save-dev

2 - Add seed in your package.json scripts:

"scripts": {
  "seed":"esbuild prisma/seed.ts --outfile=node_modules/tmp-seed.cjs --bundle --format=cjs --external:prisma --external:@prisma/client && node node_modules/tmp-seed.cjs --preview-feature"
}

3 - Run seed script

npm run seed

It works in my project, hope you can run it.

You can just tell ts-node to use CJS module resolution when transpiling seed.ts by passing --compilier-options {"module":"CommonJS"} in your seed command in your package.json.

"prisma": { 
    "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts" 
},

Still running into this issue with the latest version of Prisma while using Sveltekit. I’ve installed all the prerequisites.

I was running into the same issues but had success with tsx.

npm install -D tsx
npx prisma db seed

Svelte version: "svelte": "^4.0.5",

Still running into this issue with the latest version of Prisma while using Sveltekit. I’ve installed all the prerequisites.

Environment variables loaded from .env
Running seed command `ts-node prisma/seed.ts` ...
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/username/development/northbound/apps/geekd-samples/prisma/seed.ts
    at new NodeError (node:internal/errors:393:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:75:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:114:38)
    at defaultLoad (node:internal/modules/esm/load:81:20)
    at nextLoad (node:internal/modules/esm/loader:161:28)
    at ESMLoader.load (node:internal/modules/esm/loader:594:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:446:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:469:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:423:34) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

An error occurred while running the seed command:
Error: Command failed with exit code 1: ts-node prisma/seed.ts

I’ve tried it multiple ways

"prisma": {
    "seed": "ts-node prisma/seed.ts"
  },

and

"prisma": {
    "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
  },

Not sure what else to try here.

TSX worked for me in sveltekit

yarn add -D tsx

“prisma”: { “seed”: “tsx prisma/seed.ts” }

After trying for a few hours, this also worked for me with typescript. I’m also using SvelteKit.

I’m using Vue3 and Vite (not Svelte) and the tsx solution worked for me.

You can just tell ts-node to use CJS module resolution when transpiling seed.ts by passing --compilier-options {"module":"CommonJS"} in your seed command in your package.json.

"prisma": { 
    "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts" 
},

Only solution that works for me on Next 13.4.12

I am facing the same issue with Prisma 3.11.1 but removing "type": "module" solves the issue.

Running seed command `ts-node prisma/seed.ts` ...
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /prisma/seed.ts
    at new NodeError (node:internal/errors:372:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:76:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:118:38)
    at defaultLoad (node:internal/modules/esm/load:21:20)
    at ESMLoader.load (node:internal/modules/esm/loader:407:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:22)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:345:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:304:34)
    at async Promise.all (index 0) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

An error occured while running the seed command:
Error: Command failed with exit code 1: ts-node prisma/seed.ts

🗒️ We heard the feedback and we are planning a redesign of db seed (currently planned to be released early September)

How it will work is that a prisma.seed property in the package.json of your project will be required if you want to use prisma db seed.

This property is where you will be able to put any command you want to execute your seed script.

More info/details & how to try it out now in: https://github.com/prisma/prisma/issues/8732