0

I am trying to deploy my Express REST API to Vercel, but I am facing a 500: INTERNAL_SERVER_ERROR (Code: FUNCTION_INVOCATION_FAILED) error on the 'Functions' tab of my deployment:

2023-02-25T11:38:34.982Z    553b0c7a-952a-4944-8842-acf218dde8a0    ERROR   No exports found in module "../src/index.js".
2023-02-25T11:38:34.983Z    553b0c7a-952a-4944-8842-acf218dde8a0    ERROR   Did you forget to export a function or a server?
RequestId: 553b0c7a-952a-4944-8842-acf218dde8a0 Error: Runtime exited with error: exit status 1
Runtime.ExitError

This is my index.js file:

/* eslint-disable no-console */
import dotenv from 'dotenv';
import mongoose from 'mongoose';
import app from './app';

dotenv.config();
const { PORT, DATABASE_URL } = process.env;

mongoose.connect(
  DATABASE_URL,
  (error) => {
    if (error) {
      console.log('Fail connection to database', error);
    } else {
      console.log('Connected to database');
      app.listen(PORT, () => {
        console.log(`Server ready on port ${PORT}`);
      });
    }
  },
);

This is my package.json file:

{
  "name": "backend-template",
  "version": "1.0.0",
  "description": "app_name is a system that provides Human Resources services to companies.",
  "main": "index.js",
  "scripts": {
    "test": "jest --passWithNoTests",
    "start": "nodemon --exec npm run babel-node -- src/index.js",
    "lint": "eslint .",
    "lint:fix": "eslint . --fix",
    "babel-node": "babel-node --presets=@babel/preset-env",
    "prepare": "husky install"
  },
  "author": "",
  "license": "ISC",
  "engines": {
    "node": "14.x"
  },
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.0",
    "firebase-admin": "^11.3.0",
    "joi": "^17.6.3",
    "mongoose": "^6.6.5",
    "nodemon": "^2.0.15"
  },
  "devDependencies": {
    "@babel/core": "^7.17.9",
    "@babel/node": "^7.16.8",
    "@babel/preset-env": "^7.16.11",
    "eslint": "^8.14.0",
    "eslint-config-airbnb-base": "^15.0.0",
    "eslint-plugin-import": "^2.26.0",
    "husky": "^7.0.4",
    "jest": "^29.2.1",
    "mongodb-memory-server": "7.5.0",
    "supertest": "^6.3.0"
  }
}

And finally this is my vercel.json file:

{
  "version": 2,
  "builds": [
    {
      "src": "src/index.js",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "src/index.js"
    }
  ]
}

I have checked my index.js file but I don't know what should I export. I only use it to connect to my MongoDB server with Mongoose. I have also verified that I have all the required dependencies installed and specified in my package.json file. However, I am still getting this error when I try to deploy.

I have also double-checked my Vercel deployment settings to ensure that I have specified the correct entry point for my Express server, and I also checked that all of my environment variables are correctly set.

I expected my deployment to run correctly as I have no issues running the server locally but got a 500: INTERNAL_SERVER_ERROR (Code: FUNCTION_INVOCATION_FAILED) on the Vercel deployment.

Any help or suggestions would be greatly appreciated. Thank you in advance!

1 Answers1

2

I fixed it! The solution was adding the following code (that was on the app.js file before) inside of the index.js file:

const app = express();

app.use(express.json());
app.use(cors());
app.use(router);

export default app;