Running npx prisma db seed after deployment

I’m able to deploy a remix app, however its not running my seed.ts file (per the Remix | Jokes App) instructions.

I’d like to run the seed.ts on the deployed instance after the deploy has finished. I’m able to ssh to the app and run npx commands, but when i run “npx prisma db seed” I get the error:

Running seed command node --require esbuild-register prisma/seed.ts
node:internal/modules/cjs/loader:936
throw err;
^

Error: Cannot find module ‘esbuild-register’
Require stack:

  • internal/preload
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at Module._preloadModules (node:internal/modules/cjs/loader:1282:12)
    at loadPreloadModules (node:internal/bootstrap/pre_execution:539:5)
    at prepareMainThreadExecution (node:internal/bootstrap/pre_execution:85:3)
    at node:internal/main/run_main_module:7:1 {
    code: ‘MODULE_NOT_FOUND’,
    requireStack: [ ‘internal/preload’ ]

I’ve tried installing esbuild but that doesn’t seem to work. Does anyone have any info on how to get this to work? Thanks

That npx command probably relies on dev dependencies that aren’t present when deployed. A few ideas:

  1. Maybe try running npm install on the instance and try again. You could also install esbuild-register but who knows what other dependencies will be missing afterwards.

  2. Run the seed command in your Dockerfile and check everything is working locally then deploy using your updated Dockerfile.

  3. Deploy a fly db, proxy it, and run this seed command against your local deps. Then deploy the app connected to the pre-seeded DB.

Option 2 is great for getting things up and running reliably, but 3 makes sense if you need to scale this app to more instances and you don’t want to include data seeding in the image.

ok, will try those options - thanks for the response

1 Like

@dreday did you manage to resolve it? I’m currently experiencing the same issue and have not been able to resolve it myself.

My seed.ts is importing some json-data. I’ve rewritten them to now use require, but whenever I run it now fly crashes due to out of memory…

Not really sure how to proceed

Hi all, for reference, I got it working after some trial and error.
@jmsfbs answer guided me a bit.

So what I do is have the prisma in package.json be

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

This seed file uses data files via require and the data files must be included in the build (you could put it in public or in /prisma for example). There is no seeding when I deploy with flyctl deploy. When deployed however, then I do flyctl ssh console to ssh into the current app, then cd myapp then i run npx ts-node prisma/seed.ts which asks me if I want to install ts-node, y → seeding is starting.

I know this is not ideal, but I was getting frustrated by my total inexperience with docker and build processes, so it ended up being just fine sshing into production and running scripts there.

My git repo if anyone would like to explore further GitHub - RobertZetterlund/sommarprat-ui: remix frontend of sommarprat

1 Like

I moved ts-node from devDependencies to dependencies and got it to work.

2 Likes

Thanks that worked!

Did you have to move also typescript (peer dependency of ts-node) as well?

I couldn’t get any of the above solutions to work… Instead, what I did was compiled the seed to .js, and then in my fly.toml file, I set my release command to be: release_command = "bash -c 'npx prisma migrate deploy && npx node prisma/seed.js'". This seemed to work!

Thanks for this, yeah frustratingly none of these worked for me either so I did the same as you. It definitely feels like this should be easier somehow. Running npm install (somedep) through fly console didn’t seem to do anything for some reason.

Without seeing your app or configuration, I can’t provide specific advice, but what I can do is show you a working app:

Feel free to skip sections and go straight to the ORM section where --prisma is described.

Hey, thanks for the reply. I had a look at the post you linked but unfortunately couldn’t see anything that solves my initial issue. Still, as I say, @wengzilla’s post helped me out, I’m just a bit surprised running npm install doesn’t do anything: