'Is it possible to deploy a NodeJs app in Vercel?

I'm trying to deploy an API (made in Node) at Vercel (https://vercel.com, before Now) from the CLI. But when I deploy the app, I open the site and the result is just the files in the path directory, and not the app running. This is my server.js

    {
  "name": "subtitles-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "pre-deploy": "node deleteLastDeploy.js",
    "deploy": "npm run pre-deploy && now --public && now alias",
    "test": "echo \"Error: no test specified\" && exit 1"
  }
  "engines": {
    "node": ">=6.9"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    //list of dependencies
  }
}

To see the full API: https://github.com/bitflix-official/subtitles-api



Solution 1:[1]

Run yarn global add now@latest to install the CLI

  1. Create a now.json file and paste this
{
  "version": 2,
  "builds": [{
    "src": "./server.js",
    "use": "@now/node-server"
  }],
  "routes": [{"handle": "filesystem"},
    {
      "src": "/.*",
      "dest": "server.js"
    }
  ]
}

Note: Change "src": "server.js", && "dest":"server.js" to your server entry file.

  1. Add it to .gitignore

  2. Then run now in the CLI to deploy.

If you are deploying to production use now --prod command in the CLI to deploy

Here is an example server that I deployed: https://vercel-example-server.now.sh.

Solution 2:[2]

For the time being, with Vercel it's not possible to have a server-run web app that relies on Node.

Vercel is a cloud platform for static frontends and serverless functions.

In order to deploy a node api with Vercel you would need to use their serverless functions.

Solution 3:[3]

You should look at https://github.com/Chuloo/now-express repository for their serverless boilerplate code

Solution 4:[4]

If you're using ExpressJS, if you try to deploy your app to Vercel according to their docs, you'll have only one single serverless function /api. But this article helped me deploy separate function for each route in my Express app. So you might e able to deploy a NodeJS app as well. I found it easy to do.

See this you can deploy NodeJS App by splitting routes into functions.

https://antappan.medium.com/deploy-express-project-with-multiple-routes-to-vercel-as-multiple-serverless-functions-567c6ea9eb36

Solution 5:[5]

Something important to take in consideration about the Vj Abishek's answer, @now/node-server is depreceated since decembre 31 2020

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 alewis729
Solution 3 Viraj Singh
Solution 4
Solution 5 Charly Escalona