0
0
AzureHow-ToBeginner · 4 min read

How to Deploy Node.js to Azure App Service Easily

To deploy a Node.js app to Azure App Service, first create an App Service instance in the Azure portal, then push your app code using Git or Azure CLI. Azure automatically detects the Node.js app and runs it using the specified start script in package.json.
📐

Syntax

Deploying a Node.js app to Azure App Service involves these main steps:

  • Create App Service: Set up a web app in Azure portal or via CLI.
  • Prepare app: Ensure package.json has a start script.
  • Deploy code: Use Git push, ZIP deploy, or Azure CLI commands.
  • Run app: Azure runs npm start automatically.
bash
az webapp create --resource-group <resource-group> --plan <app-service-plan> --name <app-name> --runtime "NODE|16-lts"
💻

Example

This example shows how to deploy a simple Node.js app using Azure CLI and Git.

Steps:

  • Create resource group and app service plan.
  • Create web app with Node.js runtime.
  • Initialize Git repo and push code.
bash
az group create --name myResourceGroup --location eastus
az appservice plan create --name myPlan --resource-group myResourceGroup --sku B1 --is-linux
az webapp create --resource-group myResourceGroup --plan myPlan --name myNodeApp123 --runtime "NODE|16-lts" --deployment-local-git

# Get deployment URL
az webapp deployment source config-local-git --name myNodeApp123 --resource-group myResourceGroup

# Initialize Git repo
git init

# Add Azure remote URL from output
# git remote add azure <deployment-url>
# git add .
# git commit -m "Initial commit"
# git push azure master
Output
Resource group 'myResourceGroup' created. App service plan 'myPlan' created. Web app 'myNodeApp123' created with Node.js 16 runtime. Git remote URL: https://<deployment-url>.git # After git push, app is deployed and running.
⚠️

Common Pitfalls

Common mistakes when deploying Node.js apps to Azure App Service:

  • Missing start script in package.json causes app not to run.
  • Not setting the correct Node.js version in Azure runtime leads to compatibility issues.
  • Forgetting to commit and push all files, especially package.json and server.js.
  • Not configuring environment variables needed by the app.
json
{
  "scripts": {
    "serve": "node server.js"
  }
}

{
  "scripts": {
    "start": "node server.js"
  }
}
📊

Quick Reference

Summary tips for deploying Node.js apps to Azure App Service:

  • Always define start script in package.json.
  • Use Azure CLI to create and configure your app service.
  • Deploy via Git or ZIP deploy for easy updates.
  • Check Azure logs if app fails to start.

Key Takeaways

Create an Azure App Service with Node.js runtime before deploying.
Ensure your Node.js app has a proper 'start' script in package.json.
Deploy your app code using Git push or Azure CLI deployment commands.
Set environment variables in Azure if your app requires them.
Check Azure logs to troubleshoot deployment or runtime issues.