Hi all,
I’m running into a problem where my Next.js application cannot connect to my hosted MongoDB instance during build time. Every time that the app hits the compilation step for the ISR pages, I am met with the following error:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: prisma:error
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: Invalid `prisma.user.findMany()` invocation:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: Raw query failed. Code: `unknown`. Message: `Server selection timeout: No available servers. Topology: { Type: ReplicaSetNoPrimary, Servers: [ { Address: db-mongodb-nyc1-XXXXX-XXXXXXXX.mongo.ondigitalocean.com:27017, Type: Unknown, Error: timed out }, ] }`
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: PrismaClientInitializationError:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: Invalid `prisma.user.findMany()` invocation:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: Raw query failed. Code: `unknown`. Message: `Server selection timeout: No available servers. Topology: { Type: ReplicaSetNoPrimary, Servers: [ { Address: db-mongodb-nyc1-XXXXX-XXXXXXXX.mongo.ondigitalocean.com:27017, Type: Unknown, Error: timed out }, ] }`
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at Zr.handleRequestError (/workspace/node_modules/@prisma/client/runtime/library.js:171:6688)
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at Zr.handleAndLogRequestError (/workspace/node_modules/@prisma/client/runtime/library.js:171:5948)
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at /workspace/node_modules/@prisma/client/runtime/library.js:174:3087
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at async /workspace/node_modules/@prisma/client/runtime/library.js:174:3268
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at async t._executeRequest (/workspace/node_modules/@prisma/client/runtime/library.js:174:10726)
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at async t._request (/workspace/node_modules/@prisma/client/runtime/library.js:174:10455)
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at async getStaticPaths (/workspace/apps/nextjs/.next/server/pages/p/[username].js:40:19)
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at async buildStaticPaths (/workspace/node_modules/next/dist/build/utils.js:598:33)
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at async /workspace/node_modules/next/dist/build/utils.js:930:115
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at async Span.traceAsyncFn (/workspace/node_modules/next/dist/trace/trace.js:79:20) {
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: clientVersion: '4.11.0',
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: errorCode: undefined
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: }
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean:
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: > Build error occurred
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: Error: Failed to collect page data for /p/[username]
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: at /workspace/node_modules/next/dist/build/utils.js:960:15 {
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: type: 'Error'
[2023-03-23 14:37:36] │ @plicanta/nextjs:build:digitalocean: }
I’ve tried playing around with the trusted sources, but my applet is already there and the last successfully deployed instance is still capable of connecting. Additionally I can connect to the database instance both through MongoDB Compass as well as having the application build and run successfully.
For context, I’ve recently transitioned my app from a single Next.js application into a monorepo using Turborepo. My package.json
looks like this:
{
"private": true,
"workspaces": [
"packages/*",
"apps/*",
"packages/config/*"
],
"license": "UNLICENSED",
"packageManager": "yarn@1.22.19",
"scripts": {
"build": "npm run with-env turbo run build",
"build:digitalocean": "npm run with-env turbo run build:digitalocean",
"db:push": "npm run with-env turbo run db:push",
"db:generate": "turbo run db:generate",
"start": "npm run with-env turbo run start",
"dev": "npm run with-env turbo dev --parallel",
"clean": "git clean -xdf node_modules",
"clean:workspaces": "turbo clean",
"lint": "turbo lint && manypkg check",
"lint:fix": "turbo lint:fix && manypkg fix",
"format": "prettier --write \"**/*.{js,cjs,mjs,ts,tsx,md,json}\" --ignore-path .gitignore",
"type-check": "turbo type-check",
"with-env": "dotenv -e .env --"
},
"dependencies": {
"@ianvs/prettier-plugin-sort-imports": "3.7.1",
"@manypkg/cli": "^0.20.0",
"@types/prettier": "^2.7.2",
"dotenv-cli": "^7.0.0",
"eslint": "^8.34.0",
"eslint-config-acme": "0.0.0",
"prettier": "^2.8.5",
"prettier-plugin-tailwindcss": "^0.2.5",
"turbo": "^1.8.3",
"typescript": "^4.9.5"
}
}
Any help here would be appreciated, as I have been pulling my hair out trying to figure this out.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi there,
Indeed you are correct. During the build process your app would not have access to the Managed Database cluster in case that you have the trusted sources enabled.
What I would suggest is that if you have a migrations step, you could move that step from the build process to the deploy process.
The benefit of doing this is that, if your build is successful and the changes to the database are executed, but the deployment fails, then you might end up with the new database schema but with your old codebase which might not be backwards compatible.
Let me know how it goes!
Best,
Bobby
Just to update on the situation, I tried swapping out the DigitalOcean hosted DB URL with a MongoDB instance that I have running up on railway, and the result was that it deployed successfully. I’d really like to find out why this isn’t working properly on DigitalOcean.