skip to Main Content

How do I deploy a Nodejs, Express.js with Typescript app on Vercel? It’s giving me an error "404: NOT_FOUND" , My index.ts file is there inside my src folder. What is the proper way to do this?

vercel.json:

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

package.json:

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon",
    "test": "echo "Error: no test specified" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/body-parser": "^1.19.2",
    "@types/compression": "^1.7.2",
    "@types/express": "^4.17.17",
    "@types/jsonwebtoken": "^9.0.2",
    "@types/nodemailer": "^6.4.9",
    "nodemon": "^3.0.1",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.2.2"
  },
  "dependencies": {
    "@types/node": "^20.5.7",
    "body-parser": "^1.20.2",
    "compression": "^1.7.4",
    "dotenv": "^16.3.1",
    "express": "^4.18.2",
    "helmet": "^7.0.0",
    "jsonwebtoken": "^9.0.2",
    "mongoose": "^7.5.0",
    "nodemailer": "^6.9.4"
  }
}

enter image description here

2

Answers


  1. Please share the folder structure?
    A quick tip -> create api folder in root directory and create index.ts file there and move all your api functions or main index.ts functions api/index.ts file and remove server listen function, then change the vercel.config file src path to api/index.ts try to upload now.

    Login or Signup to reply.
  2. the problem is in your vercel.json, you didn’t specify the path destination (you put ‘/’, instead it should be ‘index.ts’)

    {
      "version": 2,
      "builds": [
        {
          "src": "index.js",
          "use": "@now/node"
        }
      ],
      "routes": [
        {
          "src": "/(.*)",
          // Specify the paths' destination
          "dest": "index.ts"
        }
      ]
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search