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.
-
1Hi! Could you give more details about what you're expecting as answer/advices? The process of deployment of a Nestjs app is very similar to any nodejs app, even if I'd recommend compiling your source code before starting the app (and not using ts-node, just like in the standard development mode), with a well defined `tsconfig.json` dedicated to production releases (do not emit the source maps unless you have a good reason, same for the declarations, strip comments, etc) – VinceOPS Dec 27 '18 at 14:34
-
Am expecting a good guide article/answer on how to. Or sample of production ready tsconfig.json with webpack tool. – Boobalan Dec 28 '18 at 13:35
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);

- 54,283
- 17
- 197
- 195
-
1I have run `npm run build` and system create a folder named `dist`. But when I execute `node dist/main.js`, the cmd shows `Error: Cannot find module 'src/article/article.entity'`. In develop mode it's normal. – Eve-Sama Sep 15 '19 at 13:55
-
4@Eve NestJS can't handle absolute paths like this by default. Replace it with a relative path like '../../article/article.entity'. When you do a production build like this, it moves all your files into a folder called 'dist' which does not contain a 'src' folder in it, which is why it says it can't find that file when you try to run your production build. – Shasta Kota Jan 03 '20 at 17:45
-
1Important addition: the default .gitignore includes the /dist folder. This needs to be removed in order for the heroku deploy to work. – Nils Apr 05 '21 at 21:34
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.

- 21,553
- 9
- 123
- 126