Deploy Nest Js applications to Railway.app
Tags: nestjs, node, railway, deploy
Published: 2022-10-11 22:53:03

You can check my original post at [Medium](https://ronald-flores-s.medium.com/deploy-nest-js-application-to-railway-app-d21b654a00a4)

## Deploy Nest Js applications to Railway.app

This would be a short guide to successfully deploying your NodeJS/NestJS app to **Railway**.

I’ll assume that you:

* Have knowledge of Git.

* A **GitHub** account.

* Have knowledge of NodeJS/NestJS.

* A **[Railway](https://railway.app/)** account.

## Steps:

* Create a local project.

* Upload to a git repository, (**GitHub** in this case).

* Create a project in **[Railway](https://railway.app/)** and connect it with our repo.

### Let’s start by creating a new NestJs:

If you don’t have NestJS CLI installed, just run this command.

    npm i -g **@nestjs/cli**

To create a new project, run the following command.

    **nest** new project-name

That would give us a basic NestJs application that we could deploy to set up the workflow.

In our project, all we need to do is to open

    ./src/**main**.ts

and update the following line that specifies the port we are running on.

    await app.**listen**(3000);

to:

    await app.**listen**(process.env.PORT || 3000);

This would take the port from our cloud env or the .env file, just with this our application is ready to run in the **Railway’s** cloud.

![](https://cdn-images-1.medium.com/max/2000/1*pNty4v-bJjXVyUr37IuL1A.png)

### Upload to GitHub:

Now we can upload our local project to GitHub, just create an empty repo in **GitHub**, init your local and push your **initial commit**. 
Once you have your project in **Github**, all that’s left is to connect it to a project in **Railway**.

### Let’s deploy to Railway:

The first step is to create a new project, from the **GitHub** repo.

![](https://cdn-images-1.medium.com/max/2998/1*V9k2-ThOX3On8NXhJzrY0Q.png)

Selecting our repo:

![](https://cdn-images-1.medium.com/max/2998/1*8OaNWgmcweav1gbpH8kfmg.png)

By default, it would deploy on commits at the **main** **branch**, but we can change that in the settings.

![](https://cdn-images-1.medium.com/max/3746/1*gZe1vlaRC1PeLqYiTX0XQA.png)

Here we can update the branch that would trigger the deployment and also add or generate a domain for this app:

![](https://cdn-images-1.medium.com/max/2344/1*jjMJ9_uGsjvIX93wa-uQ0A.png)

Also, we can specify the command to run our app.

![](https://cdn-images-1.medium.com/max/2344/1*6m340tdCHyMIrTB2L9pK3A.png)

Now we have successfully deployed our **NestJs** app.

![](https://cdn-images-1.medium.com/max/2344/1*T_cybcrrJw5ooc3XE8Bqlg.png)

Now you could just test the default response from the app:

![](https://cdn-images-1.medium.com/max/2000/1*C2J-Or_VJoIB85ofZXorlw.png)

Just remember that you need to update the port to use your **env**, so you don’t get this error message.

![](https://cdn-images-1.medium.com/max/2344/1*NXlbCaTFIsZU8hI8rKoUCQ.png)

### Bonus:

Since the Port **env** variable is created by **Railway** we don’t need to add any variable for this example, but in case you need to add them, just go to the **Variables** tab and add them.

![](https://cdn-images-1.medium.com/max/2354/1*heUnM7Qf3S3-4ujBPzA5BQ.png)

![](https://cdn-images-1.medium.com/max/2354/1*BEcACGkAnrWn7Docq6w7TQ.png)

### Recap:

With a few steps, you can have your **NestJs** Application up and running in **[Railway](https://railway.app/)**, just remember to point your port to the **env** so you don’t have an error, the app would be running, but not connecting to the open port.