skip to Main Content

The nest js build it’s not including the package.json which seems to be normal, but then when deploying to azure web app, the server it was not capable to install the needed packages. Finally I have uploaded manually the package.json file and installed manually the dependencies, but I am sure this is not the way to do it. What is the correct way to deploy a nest.js app?

I deployed my dist folder as generated from the build and I expected the azure web app to be able to start my app without further manual workarounds.

2

Answers


  1. If you are planning to deploy from Azure DevOps follow these steps. Medium

    Login or Signup to reply.
  2. I created Sample nestjs App and deployed to the Azure web app (Linux) through VS code.

    Thanks @Azure OSS Developer Support for Clear Explanation.

    • When you deploy the nestjs app to azure, the dist folder and package.json are needed, So Entire Project needs to deploy.

    Below is My complete Project Structure.

    enter image description here

    • This is because Azure Web App needs the package.json file to know which dependencies to install. Without package.json, which leads to runtime errors.

    • The Nest project created listens on port 3000 by default. But in Azure Web App dynamically assigns a port number to application at runtime. This port is provided via the PORT environment variable.

    enter image description here

    So, I define the port in main.ts file as shown below.

    const port = process.env.PORT || 3000;
    

    Below is the complete code.

    main.ts:

    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app.module';
    const port = process.env.PORT || 3000;
    async function bootstrap() {
      const app = await NestFactory.create(AppModule);
      await app.listen(port);
    }
    bootstrap();
    

    Make sure to add the start script to the package.json as shown below.

    "start:prod": "node dist/main"
    

    In VS code select Angular Extension -> Subscription -> Azure Web App -> Deploy to Web App -> Complete Project Folder as shown below.

    enter image description here

    It is Successfully deployed to Azure Web app.

    enter image description here

    Azure Web App Output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search