Deploying NestJS Apps to Zeit Now.

Mark Pieszak | Trilon Consulting
Mark Pieszak

Part 1 in a series of Articles about how to Deploy NestJS to Production.

In this article we'll be looking at how to deploy NestJS applications to the cloud platform Zeit Now in only a few minutes!

See the "Hello World" NestJS + Zeit Now live demo:
NestJS-Zeit.Now.sh


You can find the Github code example here

What is Zeit "Now" ?

Zeit Now is a cloud platform for serverless deployment. It's an incredibly simple, easy to use platform that allows you to deploy anything from static websites to server/serverless application instantly, scale automatically, all with minimal configuration.

This includes front-end applications (Angular/React/Vue/etc), or any backend of your choosing - Go, Node.js, Python and everything in-between!

NestJS is a Node.js framework after all, so how can we take advantage of an incredible Cloud platform like Now, and deploy our applications ?

Getting setup

NOTE: In this demonstration, we'll be showcasing a new NestJS application generated by the CLI, but if you prefer to use an existing NestJS application - feel free - and just skip ahead !

Generate a new NestJS Application

For demo purposes, let's make sure we have the latest NestJS CLI installed - and create a New application.

npm i -g @nestjs/cli
nest new PROJECT_NAME

Now let's cd into the newly created directory and open up our IDE. At this point we have a simple generated NestJS Application.

Setting up Zeit Now

# Install the Now CLI
npm i -g now

Make sure you're logged into the Now CLI (or create an account before logging in).

now login
# enter email & password

Ok great! We have a "hello world" NestJS application & Now setup, where do we go from here?


Configuring Zeit Now for NestJS

Typically with Now, deployments are as simple as typing now in your terminal.

But that alone won't work for our NestJS application.


Now lets you configure your Deployment Configuration via a now.json file (typically found in the root of a project).

Now.json configuration

With this now.json configuration file, we can control many aspects of our deployment:

  • Deployment / Project Name
  • Aliases (ie: your domain URL)
  • Build setup
  • Routing
  • Serving static assets
  • much more...

At the root of your application, create a now.json file and add the JSON code below.

  • Note: You can set name below to whatever you would like
{
"version": 2,
"name": "nestjs-now",
"builds": [
{
"src": "dist/main.js",
"use": "@now/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "dist/main.js"
}
]
}

What is this configuration doing?

  • Builds
    • On the line "use": "@now/node", we are telling the Now builder to take the file dist/main.js as an entry point for a Node.js function, building its dependencies, and bundling them into a Lambda.
    • Let's remember that at the end of the day, NestJS compiles down to JavaScript and runs like a standard Node.js server
      • By default NestJS is using Express behind the scenes, but it can optionally be switched to use Fastify.
    • More info on @now/node here
  • Routes
    • We want to make sure that all routing /(.*) is handling by the API routes we setup within our NestJS application, so we're just telling Now where our main file is.
    • More info on Now routes here

Building & Deploying

Now that we have everything setup - let's Deploy it to Now!

NestJS is a TypeScript-based Node.js framework, so we're going to need to make sure we build it for Production (via npm run build) and then we can let Now do its thing (via now) !!

npm run build && now
# ---------------------
# example output
# ---------------------
> zeit-now-nestjs@0.0.1 build /Users/Documents/Trilon/zeit-now-nestjs
> tsc -p tsconfig.build.json
> Deploying ~/Documents/Trilon/zeit-now-nestjs under trilon-io
> Using project nestjs-now
> Synced 2 files (462.27KB) [2s]
> https://nestjs-zeit.now.sh/ [v2] [928ms]
> Ready! Aliased to https://nestjs-zeit.now.sh/ [in clipboard] [43s]

NestJS deployed to the ☁ !

If you look above (or in your terminal if you're following along), we can see there was a URL outputted in the terminal! It was automatically copied to our clipboard, so go ahead and open up a Browser and take a look!!

"Hello World" in all of its magical glory...

NestJS Zeit Now deployment example

NestJS Zeit Now deployment example

NOTE: Inspecting the page itself we can see that it was infact served up by an Express server, just as a default NestJS application is setup to do!

There we have it!

In just a few minutes and one small json file - we took our NestJS application to the cloud with Zeit Now!


See the "Hello World" NestJS + Zeit Now live demo:
NestJS-Zeit.Now.sh


You can find the Github code example here


In Conclusion

  • Now makes deploying our NestJS applications to the cloud even simpler.
  • Make sure your scripts are building your NestJS before deploying.
  • Setup your now.json to be configured for NestJS builds.
  • Enjoy the ☁ responsibly!

Learn NestJS - Official NestJS Courses 📚

Level-up your NestJS and Node.js ecosystem skills in these incremental workshop-style courses, from the NestJS Creator himself, and help support the NestJS framework! 🐈

🚀 The NestJS Fundamentals Course is now LIVE and 25% off for a limited time!

🎉 NEW - NestJS Course Extensions now live!
#NestJS
#Zeit
#Now
#Deployments

Share this Post!

📬 Trilon Newsletter

Stay up to date with all the latest Articles & News!

More from the Trilon Blog .

Jay McDoniel | Trilon Consulting
Jay McDoniel

NestJS Metadata Deep Dive

In this article we'll be doing a deep-dive and learning about how NestJS uses Metadata internally for everything from dependency injection, to decorators we use everyday!

Read More
Kamil Mysliwiec | Trilon Consulting
Kamil Mysliwiec

NestJS v10 is now available

Today I am excited to announce the official release of Nest 10: A progressive Node.js framework for building efficient and enterprise-grade, server-side applications.

Read More
Manuel Carballido | Trilon Consulting
Manuel Carballido

Implementing data source agnostic services with NestJS

Learn how to implement data source logic in an agnostic way in yours NestJS applications.

Read More

What we do at Trilon .

At Trilon, our goal is to help elevate teams - giving them the push they need to truly succeed in today's ever-changing tech world.

Trilon - Consulting

Consulting .

Let us help take your Application to the next level - planning the next big steps, reviewing architecture, and brainstorming with the team to ensure you achieve your most ambitious goals!

Trilon - Development and Team Augmentation

Development .

Trilon can become part of your development process, making sure that you're building enterprise-grade, scalable applications with best-practices in mind, all while getting things done better and faster!

Trilon - Workshops on NestJS, Node, and other modern JavaScript topics

Workshops .

Have a Trilon team member come to YOU! Get your team up to speed with guided workshops on a huge variety of topics. Modern NodeJS (or NestJS) development, JavaScript frameworks, Reactive Programming, or anything in between! We've got you covered.

Trilon - Open-source contributors

Open-source .

We love open-source because we love giving back to the community! We help maintain & contribute to some of the largest open-source projects, and hope to always share our knowledge with the world!

Explore more

Write us a message .

Let's talk about how Trilon can help your next project get to the next level.

Rather send us an email? Write to:

hello@trilon.io
© 2019-2023 Trilon.