Reputation: 319
Hi guys i am trying to deploy my MERN app to vercel, but i have a problem when i try to deploy the backend i get an error, even though when i tried it on the local server there was no error at all
can you help me to fix this ?
**here is the error : **
This Serverless Function has crashed.
Your connection is working correctly.
Vercel is working correctly.
500: INTERNAL_SERVER_ERROR
Code: FUNCTION_INVOCATION_FAILED
my index.js :
import express from "express"
import mongoose from "mongoose"
import cors from "cors";
import dotenv from "dotenv";
import UserRoute from "./routes/UserRoute.js"
import ContentRoutes from "./routes/contentRoute.js";
import MessageRouter from "./routes/MessageRoute.js"
import connectDB from "./mongodb/connect.js";
dotenv.config();
const app = express();
mongoose.connect
app.use(cors(
{
origin: {"https://hotel-backend-xi.vercel.app"},
methods: {"POST", "GET", "DELETE", "PUT"},
credentials: true
}
));
app.use(express.json({ limit: "25mb" }));
app.use(UserRoute);
app.use(ContentRoutes);
app.use(MessageRouter);
app.get('/', (req, res) => {
res.send("hello hotel")
console.log("Hello hotel")
})
const startServer = () => {
try {
connectDB(process.env.MONGODB_URL)
app.listen(5000, () => {
console.log("Server listening on 5000 http://localhost:5000");
});
} catch (err) {
console.log(err)
}
}
startServer();
here is my dependencies (package.json) :
{ "name": "backend", "version": "1.0.0", "description": "", "type": "module", "main": "index.js", "scripts": { "start": "nodemon index", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "bcrypt": "^5.1.0", "bcryptjs": "^2.4.3", "cloudinary": "^1.37.3", "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", "joi": "^17.9.2", "joi-password-complexity": "^5.1.0", "jsonwebtoken": "^9.0.1", "mongoose": "^7.3.1", "multer": "^1.4.5-lts.1", "uuid": "^9.0.0" } }
i have tried several ways, but it doesn't work i hope you can help me to fix this problem
Upvotes: 3
Views: 18819
Reputation: 1
I had the same Problem but I fix it by putting this vercel.json code on mine
{
"builds": [
{
"src": "server.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/server.js",
"methods": ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
}
]
}
and it did solved the problem
Upvotes: 0
Reputation: 1
I had the same issue, and the solution that worked for me was deleting the .vercel folder from my repository. Once I deleted it, I tried deploying via GitHub again, and it worked.
Upvotes: 0
Reputation: 1
same issue again -->This Serverless Function has crashed.
Your connection is working correctly.
Vercel is working correctly.
500: INTERNAL_SERVER_ERROR Code: FUNCTION_INVOCATION_FAILED ID: bom1::prgwx-1723364283386-665a054597f3
If you are a visitor, contact the website owner or try again later. If you are the owner, learn how to fix the error and check the logs.
Upvotes: -1
Reputation: 1
For me , go to the Redeploy to Production(by clicking three dot) option and revisit the site
Upvotes: 0
Reputation: 1
I did fix issue now because vercel doesn’t allow to upload node modules. It consider itself node modules. So I deleted the node_module file in the github repo and the problem was solved
Upvotes: 0
Reputation: 11
What worked for me was making the name of my mongodb uri in my .env the same in the environment variables on vercel thus mine was: MONGODB_URI so I wrote the same thing on vercel there I added my connection string. Find out more on: why-does-my-serverless-function-work-locally-but-not-when-deployed
Upvotes: 1
Reputation: 31
It has a simple Solution Go to MongoDB in your Project Find Network Access in security tab ,
This Solved My problem.
Upvotes: 3
Reputation: 7787
You're trying to run a web server, not a serverless function. You can run an Express app as a function, but what you have currently would need to be run on a conventional server, not Vercel.
Upvotes: -1