← Back to Guides
Profile image for Thomas Smyth
Published 25th January 2022

Deploying NestJS 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.

This guide will show you how to set up a new NestJS project with the Nest CLI and then deploy it on Northflank.

  1. Install the NestJS command line interface (CLI) by running npm install -g @nestjs/cli.

    • For more information on the NestJS CLI, including more information on how to install it, check out the NestJS CLI documentation here.
  2. Use the NestJS CLI to 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.

    • By default, the NestJS CLI will create the new project to be used with TypeScript. If you wish to use pure JavaScript run nest new my-nestjs-app —-language JavaScript. If you do decide to use pure JavaScript, there are some additional steps required to deploy your application. Add @babel/cli as a dependency and move @babel/plugin-transform-runtime and @babel/runtime from devDependencies to dependencies. Add a new script to the package.json file called build that runs babel src -d dist.
  3. To deploy your project on Northflank, you will first create a Dockerfile that defines the environment your application will run in. Fortunately, we 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"]
  1. Now you have a project ready to deploy on Northflank, you need to push it to a Git account linked with your Northflank account. Create a new Git repository through your Git provider. Link the repository with the repository the NestJS CLI initiated for you when creating the project by running git remote add origin <Your Git repository URL>. Commit your changes and push it to the repository.

    • Not familiar with Git? Check out GitHub’s Git Guide here.
    • For more information on how to link a Git account to your Northflank account, check out our documentation here.
  2. Create a combined service. Select the repository you just created. Select the “Dockerfile” build type and click “Verify”.

    • A combined service handles both the building and deployment of your project automatically everytime you push to your Git repository.
    • Port 3000 will automatically be exposed for the HTTP protocol.
  3. When you create a combined service, a build will immediately commence. Once the build has completed, you can access your application via the link in the top right corner.

    • Want to make your application look more professional by using your own domain? Check out our documentation on how to do this for free here.

Using Northflank to deploy your NestJS project

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 VSC: GitHub, GitLab or Bitbucket
  • Observe & monitor with real-time metrics & logs
  • Low latency and high performance
  • Private and optional public load balancing as well as Northflank local proxy
Share this article with your network
X