Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way of production deployment of nestjs application

I've developed simple nestjs rest services. Now I am planning to deploy my app. Please help me with efficient way of production deployment of nestjs app.

like image 605
Boobalan Avatar asked Dec 27 '18 04:12

Boobalan


People also ask

How do you deploy a NestJS project?

To do this: Create a new Git repository through your Git provider. 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. Commit your changes and push to the repository.

How do I deploy NestJS app to Elastic Beanstalk?

To apply, navigate to Elastic Beanstalk, select your environment Myapp-env, select Configuration, scroll down to Load balancer and click Edit. Enter 443 for the Port, HTTPS for the Protocol, and select the certificate you just created for the SSL certificate. Select Add.


2 Answers

Own server

1) Checkout your project's repository on your server and run npm install.

2) Run npm run build which compiles your project to javascript:

rimraf dist && tsc -p tsconfig.build.json

3) Start your application with:

node dist/main.js

Serverless

zeit now

See this answer.

Heroku

1) Add the file Procfile to your project's root directory:

web: npm run start:prod

2) Add this line to your package.json's scripts:

"heroku-postbuild": "echo Skip builds on Heroku"

3) Set the port in your main.ts (or in your ConfigService)

await app.listen(process.env.PORT || 3000);
like image 169
Kim Kern Avatar answered Oct 17 '22 22:10

Kim Kern


If you create a new NestJS project via nest new project-name it will come with the needed scripts in package.json.

yarn build
yarn start:prod

The rest depends on where you want to host the app. NestJS will run on any generic-purpose hosting (Heroku, Vercel, AWS, etc).

If you want to get started with low-config and for free you could try Heroku with the following Dockerized setup:

Dockerfile.prod

FROM node:14-alpine

WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install

COPY . /app

RUN yarn build

heroku.yml

build:
  docker:
    web: Dockerfile.prod
run:
  web: yarn start:prod

Once the app is created run

heroku git:remote --app <app-name>
heroku stack:set container

In your bootstrap function (main.ts) use the PORT env var:

await app.listen(process.env.PORT || 3000);

If you're using a DB you have to set that up as well.

like image 1
thisismydesign Avatar answered Oct 17 '22 20:10

thisismydesign