By Thomas Smyth
Published 14th February 2022
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.
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.
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-appcontaining 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.
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
Dockerfilethat 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"]
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:
Create a new Git repository through your Git provider.
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.
Commit your changes and push to the repository.
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:
- Create a new service.
- Select the “Combined” service type.
- Select the repository you just created.
- 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.
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.
The full source code used in this guide can be found in this git repository.
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
Deploy NestJS with TypeScript and MongoDB on Northflank
In this guide, we will show you how to add persistence to your NestJS project through the addition of a MongoDB database. This is a continuation of the guide: Deploy NestJS with TypeScript on Northflank.
16th February 2022 • Thomas Smyth