I am refactoring my NextJS app to use a mongodb server to store the HTML content for dynamic routing/pages, by making calls to the mongoDB server using the “fetch” API.
Locally, the app behaves as expected:
- Start mongo server with mongod (mongod --config mongodb.conf --auth)
- Connect web app to mongo server by running expressJS “server.js” via node (node server.js)
- Run next build, building all dynamic pages
- Run next start
I have condensed steps 1&2 into a “npm run start-servers” package.json command
When I deploy remotely however, I either get build errors that “port 5000 is already in use” or “mongod is not a command” when I try to explicitly ensure the mongo server is running, and my deployment environment loads but shows no content. Deployment logs also show “ECONNREFUSED” errors when I try to access these blank pages, which also tells me the server isn’t really running.
Can someone please help? Is there something I’m missing to make mongo work remotely on vercel?
Project: seandre-website-thbr-frtsg92rf-apolloxi14s-projects.vercel.app
Last deployment: v0-seandre-8xk4v5ir9-apolloxi14s-projects.vercel.app
Error from last build log (https://vercel.com/apolloxi14s-projects/v0-seandre/ARRzu1aQQ4WAuoqEFSfcwxcWSfA1?filter=errors):
sh: line 1: mongod: command not found
Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (node:net:1937:16)
at listenInCluster (node:net:1994:12)
at Server.listen (node:net:2099:7)
at Function.listen (/vercel/path0/node_modules/express/lib/application.js:635:24)
at Object. (/vercel/path0/server.js:83:5)
at Module._compile (node:internal/modules/cjs/loader:1554:14)
at Object..js (node:internal/modules/cjs/loader:1706:10)
at Module.load (node:internal/modules/cjs/loader:1289:32)
at Function._load (node:internal/modules/cjs/loader:1108:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
A somewhat related thing is that the deployments seemed to be correctly showing content when I was first using “axios” to do the DB requests but when I switched to the fetch API to align more with NextJS then the vercel deployments started having these issues.