Windows Azure Startup task to run npm install to avoid deploying node modules

Deploying to Windows Azure is a lengthy task (scottgu are you listening?) and you don't want to make it even slower by uploading a package with all the node modules inside. Last week while talking to Johnny we were wondering why Azure is not doing npm install and reading the package.json by default.

It turns out that there is a heated debate apparentely in the node community about whether or not pushing node_modules to the source control is a good idea. My take is: if you wire up your dependencies against fixed versions (instead of latest or greater than) you are safe. So if you think the same, this is a small tutorial of how to configure an Azure role to do the magic. I took many interesting snippets from the great Steve Marx blog.

How to

The process consists basically of:

  1. creating a cmd that downloads npm (not installed in the Azure VM by default) and unzip it to the bin folder where node.exe is.
  2. add the package.json with your dependencies on the role root
  3. configure the startup task in the ServiceDefinition.csdef

The batch that you will run from the startup task looks like this:


cd /d "%~dp0"

if "%EMULATED%"=="true" exit /b 0

echo npm LOG
> npmlog.txt

-c "set-executionpolicy unrestricted"
.\download.ps1 ""

7za x -y 1>> npmlog.txt 2>> npmlog_error.txt
\npm install 1>> npmlog.txt 2>> npmlog_error.txt

exit /b 0


exit /b -1

There are a couple things worth mentioning

This is the download.ps1.


$url = $args[0];

function download([string]$url) {
= $url.substring($url.lastindexof('/')+1)
if (!(test-path $dest)) {
(new-object$url, $dest);

download $url

And you can use 7za to unzip or write your own unzipping function with PowerShell (thanks Lito!)

function Extract-Zip
([string]$zipfilename, [string] $destination)

= new-object -com shell.application
= $shellApplication.NameSpace($zipfilename)
= $shellApplication.NameSpace($destination)

Finally, you will have to add the startup task to the ServiceDefinition.csdef


<Task commandLine="install_nodemodules.cmd" executionContext="elevated">
<Variable name="EMULATED">
<RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />

And that's it! enjoy traveling light :)

View the discussion comments powered byDisqus