DEV Community

Cover image for Deploying a Nest.js Application on Railway
Bhavesh Yadav
Bhavesh Yadav

Posted on

Deploying a Nest.js Application on Railway

Hey folks, In this blog post, I will guide you through the easiest way to deploy your Nest.js application using Railway. By the end of this blog, you will have successfully deployed your Nest.js application in just a few minutes.


Why Railway?

Railway is a very easy service to use, if you have ever used vercel to deploy your nextjs application then you know how easy is that, railway works exactly like that, it takes your github repo builds a docker image of it and deploy it as a container on a remote server.

Don't know what docker is? Checkout my blog series on docker here.


Step 1: Installing Nest.js CLI

To begin, head to docs.nestjs.com and copy the following command to install the Nest.js CLI:

npm install -g @nestjs/cli
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating a New Nest.js Application

Next, navigate to the desired directory and run the following command to create a new Nest.js application named "nest-app", you can name it whatever you can:

nest new nest-app
Enter fullscreen mode Exit fullscreen mode

Step 3: Connecting to GitHub

Now, let's connect our Nest.js application to a GitHub repository. Create a new repository on GitHub named "nestjs-app-deploy".


Step 4: Pushing Changes to GitHub

Follow the guide provided by GitHub to initialize the repository, add a remote branch, and push your application code.


Step 5: Deploying on Railway

Now it's time to deploy our Nest.js application on Railway. Follow these steps:

  1. Start a new project on Railway.
  2. Choose the option to deploy from a GitHub repository.
  3. Configure your GitHub application.
  4. Select the repository you created earlier, "nestjs-app-deploy".
  5. Hit the "Save" button and proceed to the next step.

Step 6: Configuring Environment Variables

In our example application, By default the port is set to 3000. However, it's best to configure it through environment variables. Follow these steps:

  1. Go to your main application file.
  2. Look for the line which specifies the port as 3000.
  3. Update it to use environment variables process.env.PORT for the port.
  4. Push the changes to GitHub.

Step 7: Assigning a Domain

To access your deployed Nest.js application, you can assign a domain to it. Railway provides an option to generate a domain for your application. Just go to settings and scroll down a bit you'll see a option called Generate Domain.


Step 8: Redeploying and Setting up Port

After updating the environment variables and pushing the changes, redeploy your application on Railway. By default, Railway doesn't assign a specific port, but you can define it using variables.

  1. Go to your Railway project settings.
  2. Create a new variable called "port" and set its value to "80" (the default HTTP port).
  3. Redeploy your application.

That's It

Congratulations! You have successfully deployed your Nest.js application on Railway. Now, your application is up and running and can be accessed by the world. In just a few minutes, you have learned how to deploy a Nest.js application using Railway.

I hope you found this blog informative and easy to follow. If you have any questions or suggestions, please leave them in the comments section below.

Thank you for reading!

Stay tuned for more exciting content, in the next blog we'll deploy a real nestjs app with a postgres database on railway. See you in the next one, Till then!

Happy Coding!

Top comments (0)