Deploying NestJS Apps to the cloud with Zeit Now in 2 minutes!
Tags: nestjs, node, devops, beginners
Published: 2019-07-17 19:45:34

> **Originally published on the [Trilon Blog](https://trilon.io/blog/deploying-nestjs-to-zeit-now) by [Mark Pieszak](https://twitter.com/MarkPieszak)** 

---

## Deploy NestJS to Production Series:

- [**Part 1: Deploy NestJS to Zeit Now.sh**](https://trilon.io/blog/deploying-nestjs-to-zeit-now) _(this article)_
- [Part 2: Deploy NestJS to Azure Functions](https://trilon.io/blog/deploy-nestjs-azure-functions)
- ... more to come ...

---

## What are we trying to achieve?

In this article we'll be looking at how to deploy **[NestJS](https://nestjs.com)** applications to the cloud platform **[Zeit Now](https://zeit.co/now)** in only a few minutes!


> **See the "Hello World" NestJS + Zeit Now live demo:<br> [NestJS-Zeit.Now.sh](https://NestJS-Zeit.Now.sh/)**<br><br>You can find the Github code example [here](https://github.com/TrilonIO/zeit-now-nestjs)

# 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.

```bash
☁ 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

```bash
# Install the Now CLI
☁ npm i -g now
```


Make sure you're logged into the Now CLI (or [create an account](https://zeit.co/login) before logging in).

```bash
☁ 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](https://zeit.co/docs/v2/deployments/configuration/?query=now.json) 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...](https://zeit.co/docs/v2/deployments/configuration/?query=now.json)

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

```json
{
  "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](https://zeit.co/docs/v2/deployments/concepts/lambdas/).

  - 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](https://zeit.co/docs/v2/deployments/official-builders/node-js-now-node/)_


- **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](https://zeit.co/docs/v2/deployments/routes/)_

---

# 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`) !!

```bash
☁ 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](https://thepracticaldev.s3.amazonaws.com/i/76n6xmfotb7f7r6wbqgh.png)


> **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:<br> [NestJS-Zeit.Now.sh](https://NestJS-Zeit.Now.sh/)**<br><br>You can find the Github code example [here](https://github.com/TrilonIO/zeit-now-nestjs)

---

# 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!