← Back to Guides
Profile image for Thomas Smyth

By Thomas Smyth

Published 14th February 2022

Deploy NestJS with JavaScript 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 JavaScript 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 —-language JavaScript. 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. Unfortunately, the project created by the NestJS CLI uses babel-node to execute the application, which is not recommended for production environments. To get around this, we will add a build command to our application that builds the application so it can be run without babel-node. To do this:

    1. Add @babel/cli as a dependency by running npm install @babel/cli.

    2. In the package.json file move @babel/plugin-transform-runtime and @babel/runtime from devDependencies to dependencies.

    3. Finally in the package.json file add a new script called build that runs babel src -d dist.

    Once this is done, your package.json file should contain:

    {
    "name": "my-nestjs-app",
    "version": "0.0.1",
    "description": "",
    "author": "",
    "private": true,
    "license": "UNLICENSED",
    "scripts": {
      "format": "prettier --write \"**/*.js\"",
      "build": "babel src -d dist",
      "start": "babel-node index.js",
      "start:dev": "nodemon",
      "test": "jest",
      "test:cov": "jest --coverage",
      "test:e2e": "jest --config ./test/jest-e2e.json"
    },
    "dependencies": {
      "@babel/cli": "^7.16.8",
      "@babel/plugin-transform-runtime": "7.16.0",
      "@babel/runtime": "7.16.3",
      "@nestjs/common": "^8.0.0",
      "@nestjs/core": "^8.0.0",
      "@nestjs/platform-express": "^8.0.0",
      "reflect-metadata": "^0.1.13",
      "rxjs": "^7.2.0"
    },
    "devDependencies": {
      "@babel/core": "7.16.0",
      "@babel/node": "7.16.0",
      "@babel/plugin-proposal-decorators": "7.16.0",
      "@babel/preset-env": "7.16.0",
      "@babel/register": "7.16.0",
      "@nestjs/testing": "^8.0.0",
      "jest": "27.3.1",
      "nodemon": "2.0.15",
      "prettier": "2.4.1",
      "supertest": "6.1.6"
    },
    "jest": {
      "moduleFileExtensions": [
        "js",
        "json"
      ],
      "rootDir": "src",
      "testRegex": ".spec.js$",
      "coverageDirectory": "../coverage"
      }
    }
  4. 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"]
  5. 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.

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

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