404 error when using vercel dev for simple node api

I am trying to build a simple barebones node.js API using vercel. The structure of the project is like the following:

project-root/
├── api/
│ ├── hello.js ← should map to /api/hello
│ └── index.js ← should map to /api/index
├── package.json
├── vercel.json

Here is my environment:

$ node -v
v20.19.0

$ vercel --version
Vercel CLI 41.6.0
41.6.0

package.json

{
  "name": "vercel-hello-verified",
  "version": "1.0.0",
  "type": "module"
}

vercel.json

{
  "version": 2,
  "builds": [{ "src": "api/*.js", "use": "@vercel/node" }]
}

The content of hello.js


export default function handler(req, res) {
  console.log("📥 /api/hello was called");
  res.status(200).json({ message: "Hello from Vercel!" });
}

When I run “vercel dev” i get the api running at > Ready! Available at http://localhost:3000

But browsing the URL http://localhost:3000/api/hello given an error:

404: NOT_FOUNDCode: NOT_FOUNDID: dev1::lkub8-1744381659691-64bcbf88b31b

I have tried different options but nothing seems to work. This is a simple node API without any framework like express. Please help resolve.

What happens if you include the file extension? So it would be localhost:3000/api/hello.js instead of localhost:3000/api/hello

Including the file extension serves the file. Actually I was able to resolve this by adding routes to the configuration. Here’s the updated configuration that works:

vercel.json

{
  "version": 2,
  "builds": [{ "src": "api/*.js", "use": "@vercel/node" }],
  "routes": [
    { "src": "^/api/hello$", "dest": "/api/hello.js" },
    { "src": "^/api/test$", "dest": "/api/test.js" },
    { "src": "^/api/.*$", "dest": "/api/index.js" }
  ]
}
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.