DEV Community

Cover image for Deploying NestJS Apps to the cloud with Zeit Now in 2 minutes!
Mark Pieszak for Trilon

Posted on • Edited on • Originally published at trilon.io

Deploying NestJS Apps to the cloud with Zeit Now in 2 minutes!

Originally published on the Trilon Blog by Mark Pieszak


Deploy NestJS to Production Series:


What are we trying to achieve?

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

☁ now login
# enter email & password
Enter fullscreen mode Exit fullscreen mode

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"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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

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!

Top comments (4)

Collapse
 
ruslangonzalez profile image
Ruslan Gonzalez

Has something changed with Zeit Now implementation with @nestframework? It gets stuck on build process and does not launch the dist/main.js script.

After updating now package it does not work this way in my case.

@markpieszak

Collapse
 
carlillo profile image
Carlos Caballero

I love nestJS and this easy way to deploy.

Thanks!

Collapse
 
markpieszak profile image
Mark Pieszak

Great to hear it Carlos !!! :)
Tough to get easier than this!

Hopefully soon we'll get a now-builder setup, so this configuration will be automatically setup.

Collapse
 
jvieirar profile image
Juan Vieira

Thanks for your post.
Does this configuration gather all the API routes together in one lambda?
Is there a way to use the zeit now pattern (one route = one lambda function)?