简体   繁体   中英

Best practice for nodejs app deployment to Azure

Recently I started to work on Nodejs project, and we are using Azure Functions for this. I'm curios about app deployment to Azure.

First I tried to find a serverless plugin to install dependency packages during deployment process. Couldn't find an easy way to do this.

After some googling and reading this documentation I figured out that best practice is to install packages on build step, and deploy already generated node_modules.

Deploying the node_modules directory as part of your application increases the size of the deployment. ... however, it does guarantee that the versions of the modules used in production are the same as the modules used in development.

So I tried to follow this, and deploy small app with dependencies:

"dependencies": {
  "azure-function-express": "^1.3.0",
  "body-parser": "^1.18.2",
  "express": "^4.16.2",
  "cookie-parser": "^1.4.3",
  "cors": "^2.8.4"
}

Zip file to deploy was about 709 KB. It deployed successfully. But when I added one more package (mongoose)

"dependencies": {
  "azure-function-express": "^1.3.0",
  "body-parser": "^1.18.2",
  "express": "^4.16.2",
  "cookie-parser": "^1.4.3",
  "cors": "^2.8.4",
  "mongoose": "^5.0.11"
}

The size of zip became 2.83 mb and deployment hangs with message "Uploading function: ...", function not deployed, and no logs.

It hangs... I'm not sure what the precise reason is, but it doesn't work even with few packages, what if I add more big packages?

I kindly ask your advice how you solve such kind of issues, and which method best works for you to build and deploy Nodejs Azure Functions. Thank you!

Solution is to use webpack to bundle a lot of packages into one file. And then it's easy and fast to deploy. If you use serverless framework there is a package serverless-webpack to do the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM