I’m attempting for the first time to deploy a node.js app to Azure as an Azure App Service, but I’m facing this stubborn error when trying to run the app that I haven’t figured out how to troubleshoot. Maybe someone here has some hints on where I’m making the mistake.
To give some background:
- I’m working on a project that has a subset of directories, each of the directories being an app (server, client, …)
- The main directory is the git directory
- The nodejs app I am working on is built with typescript, and leveraging Apollo Server (which itself leverages express)
- I’m deploying to Azure via GitHub
With that in mind, my project folder structure looks like:
| main_folder
| .github
| workflows
workflow.yml
| server
| src
| dist
.env
index.ts
package-lock.json
package.json
tsconfig.json
| other_apps
.gitignore
README.md
In index.ts
I have the code relevant to running the app. The rest of the relevant files look like this:
tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "CommonJS",
"lib": [
"ES2022", "DOM"
],
"strict": true,
"rootDir": ".",
"outDir": "./dist",
"sourceMap": true,
"esModuleInterop": true
}
}
package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev --transpile-only --no-notify --exit-child index.ts",
"build": "npx tsc",
"start": "node ."
},
// ...
}
workflow.yml
on:
push:
branches: ["master"]
workflow_dispatch:
env:
AZURE_WEBAPP_NAME: my-app-name
AZURE_WEBAPP_PACKAGE_PATH: "." # set this to the path to your web app project, defaults to the repository root
NODE_VERSION: "16.x" # set this to the node version to use
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./server
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
cache-dependency-path: ./server/package-lock.json
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present
- name: Zip artifact for deployment
run: zip -r release.zip ./* .env
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v3
with:
name: node-app
path: ./server/release.zip
deploy:
permissions:
contents: none
runs-on: ubuntu-latest
needs: build
environment:
name: "Development"
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: node-app
- name: unzip artifact for deployment
run: unzip release.zip
- name: "Deploy to Azure WebApp"
id: my_app_id
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
This app works perfectly fine in local (running npm run dev
). When I push to master
in GitHub, both the build & deployment jobs work correctly. However, when I go to Azure Portal and check on the app, I see these two things:
When I open the url for the app, the page shows:
When I check the diagnostics of the app in Azure, it tells me:
The error being Error: Cannot find module '/home/site/wwwroot/index.js'
. While setting up the configurations for automatic build & deployment in GitHub I faced several issues with my project structure, as my git repository doesn’t actually contain any app (package.json), but they are instead in nested folders. However, I don’t think this error stems from that set up, but from somewhere else.
Can somebody hint me on what I’m doing wrong here? I’ll repeat that this is my first attempt at publishing a nodejs app to Azure, and even though I think I followed the docs correctly, there are chances I messed up in an obvious step. I would appreciate having anybody pointing it out if that’s the case!
I’ll be happy to add any more information to the description that might be relevant to the issue.
2
Answers
I have tried to reproduce this by creating a sample node.js webapp and pushed the code to github.
Created an Azure App service with github actions CI/CD pipelines and deployed successfully.
Make sure you have the below Project Structure:
Created a secret in GitHub by using the publish profile of azure app service from portal as shown in below image:
After configuring the above steps, Push the code from local repo to git repo then it will trigger the github pipelines and code will be deployed to azure app service as shown in below screenshot:
You can go to the azure portal and check the deployement as successful as shown below:
Output screen after clicking on the Azure app service url:
I got the same problem (Error: Cannot find module ‘/home/site/wwwroot/index.js’).
When I checked the source code, I could’t find index.js, but found server.js.
My solution: