Ensure Node.js version is consistent on Azure services and devops pipelines

Published on November 08, 2020

Tagged: #devops #azure #nodejs

Follow me on twitter for more posts like this

I got caught out with Microsoft recently setting the default version of Node.js from 12 to 14 on Azure pipelines linux build agent images (ubuntu-latest). Here are some tips for ensuring Node.js versions are set correctly in some of the popular Azure PaaS offerings.

In your package.json

You should ensure that the versions of Node.js you know that your code works with are specified to npm.

This is achieved using the engines section in your package.json.

{ "engines": { "node": "12.x" } }

A mistake I made here was that I only has a greater than requirement. So when Node.js 14 came along it broke on of the packages in my project. This was tricky to figure out, but if I had set the engines to also have a max version, npm would have highlighted that issue immediately.

Note, this will just warn you if the version of Node.js being used doesn’t match. That would be enough to highlight an issue in the deploy or build log.

If you want you can also set npm config set engine-strict true in your npm config and now your scripts will fail to start or operate if the Node.js versions don’t match.

In Azure Pipelines tasks

There is a task that you can add to a job that tells Azure Pipelines which version of Node.js to use. I had this on one job but not all of them. Make sure that you put this task on all of your jobs because each one runs on a different environment.

jobs:
  - deployment: DevDeploy
    displayName: MyDevDeploy
    environment: my-dev-environment
    pool:
      vmImage: ubuntu-latest
    strategy:
      runOnce:
        deploy:
          steps:
            - task: NodeTool@0
              inputs:
                versionSpec: '12.x'
              displayName: 'Ensure node version available'

In App Services

If you use Azure App Service for hosting you should tell it the correct version of Node.js to use. You can do this in the gui for app services.

App services Node.js configuration
App services Node.js configuration

Summary

There can be difficult to find bugs in your npm packages caused by Azure upgrading the default version of Node.js in build agents and on any of their PaaS systems. You can mitigate it by specifying the exact versions your code is designed to work with.

Hope that helps!

Darragh ORiordan

Hi! I'm Darragh ORiordan.

I live and work in Sydney, Australia building supporting happy teams that create high quality software for the web.

I also make tools for busy developers! Do you have a new M1 Mac to setup? Have you ever spent a week getting your dev environment just right?

My DevShell tooling will save you 30+ hours configuring your dev environment with all the best modern tools. Get it here

https://darraghoriordan.gumroad.com/l/devshell


Read more articles like this one...

List of article summaries

#devops

Extract user profile attributes from an Azure ADB2C tenant using the Microsoft Graph API

I had to retrieve a list of users from an Azure Active Directory B2C instance today. I thought I could just go through the Azure UI but that’s limited to short pages of data and limited attributes.

There is a CSV export provided on the UI but you won’t get the required identity objects in the csv output if you need a user’s signin email address.

I had to use the Microsoft Graph Api to get what I needed. This is a bit hacky but it does the trick!

#devops

Force restart your Azure App service site and host

Sometimes your Azure App service host will need to be restarted. You can do this but it’s hidden away in the Azure resource manager site. Here’s how to find it!

#devops

Scheduling a feature toggle using no-code with Azure Logic Apps

I use launch darkly to toggle features on an app. There is one third-party dependency that has regular scheduled maintenance and I need to toggle the feature on and off on schedule.

Launch Darkly has built in scheduling to handle this scenario but you have to be on the enterprise plan to use it. The enterprise plan is too expensive to upgrade to for scheduling alone so I needed to find a different way to automate this.

#frontend-development

Avoid rebuild of React App in every CI stage

If you have a react app you can use env vars like REACT_APP_MY_ENV_VAR in your application and React will automatically pull them in to your app when you build the production application.

This is very useful but if you have variables that change for each environment and your application build takes a long time, you might want to avoid building unnecessarily in CI. For example you might have a QA environment and a Staging environment that have different configuration.

We type-check our code on each build and that was taking 5 minutes+ to build each environment so we had to make it faster. We changed our app from using REACT_APP env vars to using a configuration file that we could quickly write to using CI.

Our CI system is Azure DevOops so the CI scripts here are specifically for Azure DevOps but they apply to most CI systems with small changes.

The real work happens in a Node.js script that would work anywhere.