Deploy Nest Js applications to Railway.app

burzumumbra

Ronald Flores Sequeira

Posted on October 11, 2022

Deploy Nest Js applications to Railway.app

You can check my original post at Medium

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

Steps:

  • Create a local project.

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

  • Create a project in Railway 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**
Enter fullscreen mode Exit fullscreen mode

To create a new project, run the following command.

**nest** new project-name
Enter fullscreen mode Exit fullscreen mode

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

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

await app.**listen**(3000);
Enter fullscreen mode Exit fullscreen mode

to:

await app.**listen**(process.env.PORT || 3000);
Enter fullscreen mode Exit fullscreen mode

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.

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.

Selecting our repo:

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

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

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

Now we have successfully deployed our NestJs app.

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

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

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.

Recap:

With a few steps, you can have your NestJs Application up and running in Railway, 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.

💖 💪 🙅 🚩
burzumumbra
Ronald Flores Sequeira

Posted on October 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related