Hey all... I'm having issues with seeding data wit...
# orm-help

Question

How to handle seeding data with Prisma and TypeScript in a project that uses ESM modules?

Answer

Prisma does not currently support ESM modules. The recommended workaround for running seed scripts with TypeScript is to compile the `.ts` file with `tsc`, execute the resulting `.js` file with `node`, then clean up the `.js` file. Alternatively, you can remove `type: "module"` from your `package.json` and use CommonJS modules instead.

d
Hey all... I'm having issues with seeding data with Prisma and TypeScript in a project that uses ESM modules (for HMR with SvelteKit/Vite). My seed script in package.json is
ts-node prisma/seed.ts
as recommended in the guide (currently seed.ts just has some console.logs for testing) and I have all the necessary dev dependencies installed. When I run
prisma db seed
I get the following:
Copy code
Environment variables loaded from .env
Running seed command `ts-node ./prisma/seed.ts` ...

An error occured while running the seed command:
Error: Command failed with ENOENT: ts-node ./prisma/seed.ts
spawn ts-node ENOENT
Trying to run the script manually with ts-node with
npx ts-node ./prisma/seed.ts
I get the following:
Copy code
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /workspace/prisma/seed.ts
    at new NodeError (node:internal/errors:371:5)
    at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
    at Loader.getFormat (node:internal/modules/esm/loader:105:42)
    at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
    at async Loader.import (node:internal/modules/esm/loader:177:17)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
Has anybody run into this and know what's the best way to handle this from Prisma's end and found an elegant solution? This seems related to a big mess with ts-node when it comes to ESM support, ([1], [2], etc.) and the only way I've been able to get seed.ts to execute has been to remove
type: "module"
from my package.json. ts-node's docs recommend using an experimental Node.js API for using ts-node as a loader, but that's literally scheduled to break soon with node 16.10 and likely again in the future. I was also hopeful module type overrides would help, but it doesn't seem to do anything for executing seed.ts without these errors. My current workaround is to compile seed.ts with tsc, execute it with node, then clean up the js file. Since the prisma.seed command only takes one command and isn't bash-like, I create a package.json script with
tsc prisma/seed.ts && node prisma/seed.js && rm prisma/seed.js
and then make that my prisma.seed script
"seed": "npm run seed-db"
. This seems kind of clunky and I don't like that I have to write and clean up a transient file.
r
@Danny 👋 Prisma doesn’t play well with ESM at the moment. Have a look at this issue for more info.
494 Views