OdeToCode IC Logo

Using npm During ASP.NET Core Git Deployments in Azure App Services

Tuesday, September 4, 2018

If you use npm to manage client-side dependencies for ASP.NET Core applications, and you deploy to Azure App Services using Git, then you need a way to run an npm install during deployment in App Services.

There are a few options available for customizing the Azure build and deployment process. One approach is to modify the deployment script Azure generates for your ASP.NET Core project. I’ve written about this previously in Customizing Node.js Deployments for Azure App Services

Of course, Node.js deployments in Azure will automatically run an npm install, so the previous example was showing how to add Webpack into the build, but you follow the same process to customize the build for ASP.NET Core.

You can also customize your build and publish operations by adding targets to the project’s .csproj file. For example:

<Target Condition="'$(DEPLOYMENT_TEMP)' != ''"
        Name="NpmInstall" 
        AfterTargets="Publish">
  <Exec Command="npm install --production"  
        WorkingDirectory="$(DEPLOYMENT_TEMP)">
  </Exec>
</Target>

The above Target will execute npm install after a dotnet publish operation in the DEPLOYMENT_TEMP folder. DEPLOYMENT_TEMP is one of the environment variables defined by Kudu in Azure.


Comments
Gravatar Thomas Freudenberg Wednesday, September 5, 2018
If you commit your `package-lock.json` too, you may call `npm ci`, which is much faster than `npm install`. See https://blog.npmjs.org/post/171556855892/introducing-npm-ci-for-faster-more-reliable for details.
Your Comment