Is it possible to deploy a NodeJs app in Vercel?

Viewed 27850

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

6 Answers

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.

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.

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

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

I just deployed a node.js app(13-09-2022) after hearing that heroku is shutting down

These are the steps I performed

1)make a vercel.json and add this

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

2)Go to your package.json and add the engine column

{
  "name": "api-make",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "14.x"
  },

3)don't forget to change the "start" in the script to your file name

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

Now you are good to go Note:I I'm not sure why but I had my file named as app.js I was getting a 404 error I did change it to app.js everywhere still I'm not sure so I had to change it back to index.js

Related