DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for How to deploy your NestJS apps on Vercel
Duc Le
Duc Le

Posted on

How to deploy your NestJS apps on Vercel

With my little knowledge, Vercel isn’t a platform for backend services, and companies often don’t pick Vercel for their deployments. According to Vercel, they claim Vercel is the platform for frontend developers.

But even frontend developers have their own desire to create their own APIs to serve their experiments ( like me ). And I often pick **NestJS **to write APIs due to its fast development and build time, easy to learn ( but hard to master )

If you deployed **React **for **NextJS **apps on **Vercel **before, you can find it pretty convenient to deploy because of Vercel’s presets for libraries and frameworks. But for **NestJS **apps, **Vercel **will recognize your repository with Others preset, and when you try to deploy it with default settings, this will happen:

Image description

To solve this is fairly easy, what you have to do is create the vercel.json file in root folder and write this:

Image description

The vercel.json configuration file lets you configure, and override the default behavior of Vercel from within your project, find more details here

Build object definition:

  • src (String): A glob expression or pathname. If more than one file is resolved, one build will be created per matched file. It can include * and **.
  • use (String): An npm module to be installed by the build process.

Route object definition:

  • src: A PCRE-compatible regular expression that matches each incoming pathname (excluding query string).
  • methods: A set of HTTP method types. If no method is provided, requests with any HTTP method will be a candidate for the route.
  • dest: A destination pathname or full URL, including query string, with the ability to embed capture groups as $1, $2…

Run Vercel deploy again, you can see that Vercel successfully deployed our apps with build outputs:

Image description

Image description
[

*Note: my vercel.json is just a basic example on how it works, you should find the configuration that best suits you and your app

You can check out my source code here](https://github.com/leduc1901/nestjs-jwt-auth)

Last Words

Although my content is free for everyone, but if you find this article helpful, you can buy me a coffee here

Top comments (3)

Collapse
 
nicostubi profile image
nicostubi

Nice article.

The config you wrote allowed me to pass the problem of the public directory.
However I'm using graphql and I have the following error when browsing the app:

`2023-01-10T17:26:08.853Z baf32391-0925-4f77-b503-ea1ba0c55cee ERROR Cannot find module 'src/common/gql.type'
Require stack:

  • /var/task/src/products/products.resolver.js
  • /var/task/src/products/products.module.js
  • /var/task/src/app.module.js
  • /var/task/src/main.js 2023-01-10T17:26:08.853Z baf32391-0925-4f77-b503-ea1ba0c55cee ERROR Did you forget to add it to "dependencies" in package.json? RequestId: baf32391-0925-4f77-b503-ea1ba0c55cee Error: Runtime exited with error: exit status 1 Runtime.ExitError`

Any ideas how to solve this? The Vercel documentation mentions some potential issues around case, bug my imports are correct with gql.type and when I look into the source folder that is deployed, the fil is present at the correct location.

Cheers,
Nicolas

Collapse
 
saralufi profile image
Sara Lufi

Thanks for this post. If you don't mind, can you do one post on how to deploy a NestJS application which is inside an Nx monorepository that already has a NextJS app or ReactJS app.

Collapse
 
lfelipessilva profile image
LuΓ­s Felipe

Thank you! i just want to ask you to put the OPTIONS method on your vercel.json file. if it is not there, the browser cannot make preflight requests, so you cannot make json requests and etc

18 Useful Github Repositories Every Developer Should Bookmark

18 Useful GitHub repositories every developer should bookmark: everything from learning resources and roadmaps to best practices, system designs, and tools.