Deploy Nest Js applications to Railway.app

Ronald
3 min readSep 16, 2022

--

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

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.

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.

--

--

Ronald
Ronald

Written by Ronald

Front-end developer with six years of experience who loves to code amazing UI.