简体   繁体   中英

Installing Node.js modules from file system on Azure Websites

I have a Node.js app running as an Azure Website and doing my deployments using Git. The app requires a module that's private (ie written by me, no relevance to anyone else and thus not available in NPM) and common to my other projects. Let's say the module is located in ./lib/mymodule and it has its own dependencies in its package.json file. The problem is I cannot figure out how to have Azure install the dependencies of my own module. I would like to avoid having to add my node_modules directory under version control (currently .gitignor 'd) or having to add the module's dependencies as my app's dependencies (which is ugly and a bit inconvenient).

It's trivial to get this working on my local dev environment. The first thing I did was simply include my own module as a local dependency in my package.json as described in https://docs.npmjs.com/files/package.json (see section "Local Paths"). A simple npm install would now install my own module nice and easy. Since I know that Azure runs npm install every time I deploy a new version, I figured this is all I need. Turns out I was wrong: local paths in package.json dependencies section were only introduced in NPM 2.0.0 and Azure is running NPM 1.4.x, so the deployment obviously failed. Ok then, can I run a different version of NPM in Azure? In theory, yes, but practice seems to be something else. I tried out https://github.com/glennblock/azure-node-runtime-selector but to no avail. Here's some of the deployment output:

remote: Handling node.js deployment.
remote: KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
remote: ............
remote: node version: 0.11.14
remote: ..............
remote: 2.1.18
remote: npm version: 1.4.6
remote: An error has occured during web site deployment.
remote: npm failed
remote:
remote: Error - Changes committed to remote repository but deployment to website failed.

Apparently it fails to install any NPM version >= 2.0.0.

Ok then, this is not so bad, I figured. I can work with an older version of NPM, I just need to run another npm install to install my own module, as NPM documentation hints. So I took the local dependency away from my package.json and simply run npm install ./lib/mymodule . Nice and easy... on my local. I dug up instructions how to run custom commands on deployment in Azure: just run azure site deploymentscript --node and edit deploy.sh it creates. Just to ensure the custom deployment script works at all, I made one deployment without editing anything. Worked ok. Then I added a line in deploy.sh (around line 111):

# 3. Install npm packages
if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then
  cd "$DEPLOYMENT_TARGET"
  eval $NPM_CMD install --production
  eval $NPM_CMD install ./lib/mymodule #This line added by me
  exitWithMessageOnError "npm failed"
  cd - > /dev/null
fi

Nope. This is what I get:

remote: Installing npm packages...
remote: npm ERR! addLocal Could not install ./lib/mymodule
remote: An error has occurred during web site deployment.
remote: npm ERR! Error: ENOENT, stat 'd:\home\site\wwwroot\lib\mymodule'
remote: npm failed
...snip...
remote:
remote: npm ERR! System Windows_NT 6.2.9200
remote: npm ERR! command "d:\\Program Files (x86)\\nodejs\\0.10.28\\node.exe" "D:\\Program Files(x86)\\npm\\1.4.9\\node_modules\\npm\\bin\\npm-cli.js" "install" "./lib/mymodule"
remote: npm ERR! cwd d:\home\site\wwwroot
remote: npm ERR! node -v v0.10.28
remote: npm ERR! npm -v 1.4.9
remote: npm ERR! path d:\home\site\wwwroot\lib\mymodule
remote: npm ERR! code ENOENT
remote: npm ERR! errno 34
remote: npm
remote:
remote: Error - Changes committed to remote repository but deployment to website failed.

This is where I got stuck. I tried playing around with the path, guessing that Windows is mangling it somehow, but haven't found a working solution yet. The weird thing is that ./lib/mymodule would seem to resolve to d:\\home\\site\\wwwroot\\lib\\mymodule which look just fine to me and DOES exist, at least after deployment when I FTP'd in to take a look.

I would appreciate any and all help in either getting NPM 2.xx working or my making custom deployment go through. Or maybe there's an alternative to managing my local modules I haven't thought of? And if all else fails, maybe I'll just commit my node_modules or manage my dependencies manually. I've already used way too much time to resolve what first seemed like a trivial issue. :)

Azure WebSites team will be shipping support for npm 2.1.17 out-of-the box by end of the month (Jan'15). Hope this will help you unblock you.

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