Reputation: 61
I was given a task to deploy a nodejs app to azure web app however I can't get it to work.
I followed the instructions from this link and everything seems to pretty straightforward.
The only difference is my package.json start script looks like the one below.
"scripts": {
"start": "node ./node_modules/@samplescope/samplefolder/sample_service.js"
},
In the sample source code from the instructions link, it has a web.config which points to server.js, so I modified that part to the location of my start up file which is "./node_modules/@samplescope/samplefolder/sample_service.js"
from this
<rule name="DynamicContent">
<match url="/*" />
<action type="Rewrite" url="server.js"/>
</rule>
to something like this
<rule name="DynamicContent">
<match url="/*" />
<action type="Rewrite" url="./node_modules/@samplescope/samplefolder/sample_service.js"/>
</rule>
I also tried to remove the web.config I created because according to this link kudu will try to generate one for you base on your package.json but still it didn't work.
I know I am doing something wrong, can someone point me in the right direction.
Also, how does "npm install" gets invoked in the azure web app?
Yes, it working locally when invoked by "npm start"
Upvotes: 1
Views: 6172
Reputation: 61
I was able to resolve this and now able to succesfully deploy my nodejs application to azure web app.
A couple of key points that helped me resolve the issue as Iam knew to kudu deployment so I am not that familiar with it.
You can get the kudu log files via https://{site}.scm.azurewebsites.net/ > Tools Tab > Diagnostic dump
Under the "Overall Process" section of the site below you will see kudu process works. This link also helped me alot understanding the kudu process. https://blog.lifeishao.com/2017/03/24/custom-nodejs-deployment-on-azure-web-app/
After syncing the application, cleaning and copying the files to the target folder, kudu will try to generate the web.config. In our case, the .js is located in one of the private node modules thus it needs to be there prior to kudu generating the web.config.
It is also recommended to create your own deploy.cmd so you will be able to control the deployment steps. You can also refer to this link for more info. https://github.com/projectkudu/kudu/wiki/Custom-Deployment-Script#custom-deployment-script-generator
If you are deploying an application with private modules, you can refer to this link for your guide. https://www.jeff.wilcox.name/2017/02/azure-private-npm/ Also, explicitly indicate the registry when doing npm install even if you already have the .npmrc in your local folder.
Lastly, if you are deploying using Azure Service App deployment in Azure Devops, don't let it generate the web.config for you and let kudu instead generate it.
Upvotes: 4
Reputation: 116
"npm install" is invoked by the Azure web app once it detects a package.json file in the push request. It installs packages listed in the "dependencies".
However, it's not perfect. One of my projects needed SQLite but it did not install. I got around this by installing all packages locally then pushing the entire "node_modules" folder to Azure using local Git
I won't recommend placing "sample_service.js" into the "node_modules" because this folder is created and modified by npm based on the dependencies listed in "package.json". Try placing "sample_services.js" outside of but in the same folder as "node_modules".
If "sample_services.js" is the back-end Javascript server, you'll need to rename it to "server.js" or "app.js" due to naming restrictions when deploying. Specify it as "main" in your "package.json" so that Azure runs it as the equivalent of "node server.js" in Windows Powershell to get your backend going and include the following code within your "server.js":
const port = process.env.PORT || 3000;
The resulting "package.json" file should look somewhat like this:
{
"name": "myProject",
"version": "1.0.0",
"engines": {
"node": "8.10.0",
"npm": "5.6.0"
},
"description": "Project X",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "me",
"license": "ISC",
"dependencies": {
"express": "4.16.3",
"sqlite3": "4.0.2"
}
}
Azure automatically chooses which version of Node.js and npm to use. If you have compatibility issues, specify the version of Node.js and npm like I did
In addition, if you used Express for routing like I did in a prior project, this code in "server.js" runs to load the default onload HTML page:
app.get('/', function(req,res){
console.log('default html loading');
res.render('./public/index.html');
});
Again, due to the same naming restrictions, I named my main HTML page as index.html
If you'd like to try out a full example or compare code, I found this guide helpful: https://www.codeproject.com/Articles/1133660/Deploy-Node-js-in-Microsoft-Azure
Cheers!
Upvotes: 3