node.jshostingvercel

Struggling to host my NodeJs backend on Vercel


As stated, I am trying to host my Nodejs backend on Vercel but I am running into issues. As far as I am aware you need to use a vercel.json file so vercel knows it's a node environment.

I originally tried to use their interface by connecting my Github project, assigning the root directory which is ./backend, followed by custom commands for npm install and node server.js. This worked and I could see my port was started on 8080 in the logs... But the build never completes, it just get stuck in the "build" process.

For context this is my file structure

I then tried a vercel.json file with this config

This completed the build step but didn't run my server and I was met with a 404 page

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

I then tried this:

And was met with yet another forever build loop but my server obviously started

{
    "version": 2,
    "buildCommand": "npm run build",
    "routes": [{"src": "/(.*)", "dest": "/"}]
}

I'm a little stumped and I know it must be something stupid but any help would be greatly appreciated, cheers!


Solution

  • SOLVED: For anyone who stumbles across this, below are the changes I made for it to be fixed.

    package.json (specifically the start command, I was using build previously)

      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node server.js"
      },

    vercel.json

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