npx prisma db seed works in development but fails during fly deploy

Here’s my seed command: node --loader ts-node/esm prisma/seeds.js

I tried many of the other ones, ts-node prisma/seeds.ts or ts-node --require tsconfig-paths/register prisma/seeds.ts, none of them worked at least in development. Basically I have a seeds.ts file, and in order to seed I copy it into a seeds.js file.

Here’s where I put the command in my Dockerfile:

# Generate Prisma Client

COPY --link prisma .

RUN npx prisma generate

# # Seeds

RUN npx prisma db seed

# Copy application code

COPY --link . .

And here is the error I get in the terminal:

=> CACHED [build 5/9] RUN npx prisma generate 
=> ERROR [build 6/9] RUN npx prisma db seed                               2.4s
------                                                                          
 > [build 6/9] RUN npx prisma db seed:                                          
1.765 Running seed command `node --loader ts-node/esm prisma/seeds.js` ...      
2.305 (node:39) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
2.305 --import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
2.305 (Use `node --trace-warnings ...` to show where the warning was created)
2.325 
2.325 node:internal/process/esm_loader:40
2.325       internalBinding('errors').triggerUncaughtException(
2.325                                 ^
2.327 Error: Cannot find module '/app/prisma/seeds.js' imported from /app/
2.327     at finalizeResolution (/app/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366:11)
2.327     at moduleResolve (/app/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:801:10)
2.327     at Object.defaultResolve (/app/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912:11)
2.327     at /app/node_modules/ts-node/src/esm.ts:218:35
2.327     at entrypointFallback (/app/node_modules/ts-node/src/esm.ts:168:34)
2.327     at /app/node_modules/ts-node/src/esm.ts:217:14
2.327     at addShortCircuitFlag (/app/node_modules/ts-node/src/esm.ts:409:21)
2.327     at resolve (/app/node_modules/ts-node/src/esm.ts:197:12)
2.327     at nextResolve (node:internal/modules/esm/hooks:833:28)
2.327     at Hooks.resolve (node:internal/modules/esm/hooks:278:30)
2.328 
2.328 Node.js v20.8.1
2.347 
2.347 An error occurred while running the seed command:
2.348 Error: Command failed with exit code 1: node --loader ts-node/esm prisma/seeds.js
------
Error: failed to fetch an image or build from source: error building: failed to solve: process "/bin/sh -c npx prisma db seed" did not complete successfully: exit code: 1

So I have two questions. What is a proper command to seed with a .ts file and how can I make that command work during deployment?

I think the crux of the issue is that the path of the command in Docker starts from /app while in development it starts from its parent folder, which is why the command fails in production. This might be a dumb question but how do you got to the parent folder in Docker? I tried prefixing the path with …/ but it didn’t do anything.

You can’t reference a parent folder in Docker due to the build context. You want to build at the root of your project and use --dockerfile path/to/Dockerfile and -c path/to/app/fly.toml

Can you show me a use case of how that’s done? I’m also not sure about changing where my project is built just for a single feature.

You literally pass those flags to your fly deploy command. It might be easier to just move the seed folder to be a sibling of your app.

That’s exactly how it was before with the prisma folder. I also tried putting the folder inside of app but always got stuff like Cannot find module '/deployment-seeds/seeds.js' imported from /app/. What are the specific flags you’re recommending?

The flags I mentioned in the first reply. Then in your Dockerfile, update your paths to be relative to the root of your project. But don’t COPY . ., copy only what you need for your app + db seeds

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.