← Back to Guides
Profile image for Thomas Smyth

By Thomas Smyth

Published 14th February 2022

Deploy NestJS with TypeScript on Northflank

NestJS is a free open-source framework for building efficient, scalable Node.js web applications. It uses modern Javascript, is built with TypeScript and combines elements of object-oriented programming, functional programming, and function reactive programming.

In this guide, I will show you how to set up a new TypeScript NestJS project using the Nest CLI and then deploy it on Northflank. The full source code used in this guide can be found in this git repository.

  1. To assist with creating new NestJS projects containing the basic setup needed to start developing an application, NestJS comes with its own CLI tool. In this guide, we will use the NestJS CLI to create your project, however, before we can use it we must first install it by running npm install -g @nestjs/cli.

  2. Now we have the NestJS CLI installed, create a new project by running nest new my-nestjs-app. Once this is complete, a new directory will be created called my-nestjs-app containing the basic setup you need to start developing your application.

    • For more information on the NestJS CLI, including more information on how to install it, check out the NestJS CLI documentation.

  3. To deploy the project on Northflank, we must first create a Dockerfile that defines the environment your application will run in. Fortunately, I have created a Dockerfile for you, add it to your project by creating a new file in the root of your project called Dockerfile that contains the following:

    # Initiate a container to build the application in.
    FROM node:14-alpine as builder
    ENV NODE_ENV=build
    WORKDIR /usr/src/app
    
    # Copy the package.json into the container.
    COPY package*.json ./
    
    # Install the dependencies required to build the application.
    RUN npm install
    
    # Copy the application source into the container.
    COPY . .
    
    # Build the application.
    RUN npm run build
    
    # Uninstall the dependencies not required to run the built application.
    RUN npm prune --production
    
    # Initiate a new container to run the application in.
    FROM node:14-alpine
      ENV NODE_ENV=production
    WORKDIR /usr/src/app
    
    # Copy everything required to run the built application into the new container.
    COPY --from=builder /usr/src/app/package*.json ./
    COPY --from=builder /usr/src/app/node_modules/ ./node_modules/
    COPY --from=builder /usr/src/app/dist/ ./dist/
    
    # Expose the web server's port.
    EXPOSE 3000
    
    # Run the application.
    CMD ["node", "dist/main"]
  4. Now we have a project ready to deploy on Northflank, we need to push it to a Git account linked with your Northflank account to allow Northflank to access it. To do this:

    1. Create a new Git repository through your Git provider.

    2. Run git remote add origin <Your Git repository URL> to link the newly created repository with the repository the NestJS CLI initiated for you when creating the project.

    3. Commit your changes and push to the repository.

    • Not familiar with Git? Check out GitHub’s Git Guide.

    • For more information on how to link a Git account to your Northflank account, check out our documentation.

  5. To build and then deploy your project on Northflank each time you update your Git repository we will use a combined service. To do this:

    1. Create a new service.
    2. Select the “Combined” service type.
    3. Select the repository you just created.
    4. Select the “Dockerfile” build type and click “Verify”.
    • Northflank will automatically expose port 3000 to allow external HTTP traffic to access your application. Check out our documentation on how ports are detected for more information.

  6. When a combined service is created, Northflank will immediately commence a build and once complete deploy the build. Once this has been completed, you can access your application via the link in the top right corner.

    • Want to ensure maximum availability of your application? Check out our documentation on how to set up health checks for your service.

    • Want to make your application look more professional by using your own domain? Check out our documentation on custom domains.

The full source code used in this guide can be found in this git repository.

Using Northflank to deploy your NestJS application

Northflank allows you to deploy your code and databases within minutes. Sign up for a Northflank account and create a free project to get started.

  • Connect with your preferred VCS: GitHub, GitLab or Bitbucket
  • Manage build arguments and environment variables using secret groups
  • Scale vertically and horizontally with multiple replicas per service
  • Observe & monitor with real-time metrics & logs
  • Create pipelines and release workflow as you grow

Share this article with your network