Store npm installation task in Visual Studio Team Services cache

After setting up a private agent in VSTS and globally installing NPM, I am facing an issue where each build task is taking around 12 minutes to install NPM packages. This extended time is affecting the efficiency of my builds.

Is there a way to cache NPM installations in order to reduce the build time?

Answer №1

Our development process includes the use of a tool called npm-cache, which is a node module that generates a hash of the package.json file to create a zip folder containing the node_modules content. This allows us to significantly speed up the npm install process by simply extracting a zip file during each build, as long as there are no changes to the package.json file.

The concept behind this approach is that when the tool initially downloads the npm packages, it stores them locally. During subsequent builds, if the package.json file remains unchanged, the tool retrieves the packages from the local disk and copies them to the build agent folder. It only downloads new packages from the internet if there have been changes to the package.json file.

  1. To implement npm-cache on the build machine:

    npm install npm-cache -g

  2. In the build definition, add a Command Line task with the following details: Tool:

    C:\Windows\User\AppData\Roaming\npm\npm-cache
    (or simply npm-cache if the tool has been added to the environment path variables); Arguments: install npm; Working folder: $(Build.SourcesDirectory) (where the package.json is located).

Answer №2

Microsoft has recently added a new feature (currently in beta) that allows caching in Azure DevOps pipelines. You can read more about it at this link.

Here is an example of how you can use the feature:

variables:
  npm_config_cache: $(Pipeline.Workspace)/.npm

steps:
- task: CacheBeta@0
  inputs:
    key: $(Build.SourcesDirectory)/package-lock.json
    path: $(npm_config_cache)
  displayName: Cache npm

- script: npm ci

Answer №3

Regrettably, at the moment we are unable to cache NPM installations as there is currently no built-in feature for it.

However, a user has already submitted a suggestion for this feature on UserVoice: Improving hosted build agent performance with build caches. It appears that the VSTS team is actively addressing this issue...

In the meantime, you can explore ways to accelerate NPM INSTALL on Visual Studio Team Services

Answer №4

Leverage Cache Task

Integrating caching into a pipeline involves utilizing the Cache pipeline task. This particular task functions similarly to other tasks and is included within the steps section of a job.

Below is an example configuration:

pool:
  name: Azure Pipelines

steps:
- task: Cache@2
  inputs:    
    key: 'YOUR_WEB_DIR/package.json'
    path: 'YOUR_WEB_DIR/node_modules/'

- task: Npm@1
  inputs:
    command: 'install'
    workingDir: 'YOUR_WEB_DIR/frontend'

You may also use the key YOUR_WEB_DIR/package-lock.json, but it's important to note that this file could be altered by subsequent steps like npm install, resulting in a change to its hash value as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we include additional types for external npm packages in a React TypeScript project?

Recently, I encountered an issue while using the react-microsoft-login package from npm. I included a button in the children property and received a typescript error stating that "property 'children' does not exist on type 'intrinsicattribut ...

Having trouble with a Reactjs Facebook login library - update the componentClicked function to be async

Currently, I am working on incorporating Facebook login into my React application using Redux. Within my loginUser.js file, the "FacebookLogIn" component appears as follows: <FacebookLogin appId="375026166397978" autoLoad={true} fields="name, ...

Issue: Unable to authenticate the initial certificate following the configuration of an id_rsa

Upon executing the commands below, I encountered an error while trying to start a nextJS app: ssh-keygen -t rsa -C "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2afbba7afa3abae82a7baa3afb2aea7eca1adaf">[email pr ...

Console displays errors upon downloading code from git repository

Having recently downloaded a zip file from git, I extracted all files and opened them in VS Code. Following the instructions in the readme.md file, I attempted to install all dependencies but encountered the following message: npm install up to date, audi ...

Troubleshooting the transition from React version 16.0.0 to React version 18

I am encountering issues with my package.json file and Jenkins build, which fails due to the following problems. I'm currently running npm version 8.15.0 and Node version v16.17.1, while trying to upgrade from React 16 to React 18. package.json { & ...

What could be causing the 'TypeError: Cannot read properties of null (reading 'useState')' error during the installation of my React UI package using an absolute path?

Recently, I developed a UI package for React and compiled it using the Rollup package manager. Strangely, when I publish the package to my personal Bitbucket repository and install it from the cloud with a command like npm i https:lik-to-the-repo, everythi ...

When executing the command "npm or pnpm run dev", no output is shown on the screen

I'm currently using Windows 11 and working on a Next.js project. After initializing my project with "npx create-next-app@latest", I ran the command "npm run dev" but nothing appeared even though the initialization was successful. Click here to view i ...

Every time I attempt to initialize a React project using the command "npx create-react-app," I encounter the error message "enoent ENOENT: no such file or directory."

I recently attempted to start a new React project and encountered the following issue: (node:4840) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase ...

Tips for troubleshooting React JS installation issues

PS D:\React-project> node --version v15.11.0 PS D:\React-project> npm --version 7.6.1 PS D:\React-project> npx create-react-app my-app npm ERR! code ENOTDIR npm ERR! syscall mkdir npm ERR! path C:\Users\USERNAME\AppDa ...

Error in React: Unable to import the named export 'translate' (imported as 'translate') from a module that only provides a default export

Trying to integrate a Google Translate API into my React app (using the library google-translate-api-x). It's functioning well in a regular node script but encountering an error in React: The named export 'translate' cannot be imported from ...

What is the reason behind the restriction on using capital letters in React project names?

When attempting to create a new project "newRecipeApp" in React, I encountered the following message: npx: installed 91 in 29.359s Could not create a project called "newRecipeApp" because of npm naming restrictions: * name can no longer contain capital ...

Guide to running several versions of a single Next JS application with distinct configurations simultaneously on a shared server

My goal is to run 2 instances of the NextJS app on the same machine with different configurations and ports. The first instance runs on port 3000, while the second runs on port 3001, and this setup functions correctly with the commands: next dev next de ...

Variations of a particular software package are necessary

My current project requires Expo, React, and React-Native as dependencies. The configuration in the package.jason file looks like this: "main": "node_modules/expo/AppEntry.js", "private": true, "dependencies": { "expo": "^28.0.0", "expo-three": "^ ...

Tips for uploading a jpg image to the server using react-camera

My objective is to transfer an image captured from a webcam to a Lambda function, which will then upload it to AWS S3. The Lambda function appears to work during testing, but I'm struggling to determine the exact data that needs to be passed from the ...

Implementing Next.js on the Vercel platform

I'm facing an issue deploying a Next.js app to Vercel, encountering this error during the build process https://i.stack.imgur.com/oj4XU.png Although I don't come across any errors when building locally, it triggers an error during the Vercel bu ...

Guide on exporting a reducer from a Node module built on React Redux

I am currently working on a project that requires importing a component from an external node module. Both the project and the component utilize React and Redux, and I intend for them to share the same store. Below is a snippet of the reducer code for the ...

Encountered an error while compiling following the 'npm start' command in the create-react-kotlin

Having an issue running the create-react-kotlin-app module using npm. Here's the error I'm encountering: Failed to compile multi ./node_modules/react-dev-utils/webpackHotDevClient.js ./node_modules/react-scripts-kotlin/config/polyfills.js kot ...

Switching XML to JSON using React.js

Currently, I am in the process of developing an application with a requirement to retrieve data from a web API that provides XML responses. However, my objective is to obtain this information in JSON format, but unfortunately, the API does not offer suppor ...

I am encountering an error that reads: "Error: EPERM: operation not permitted, mkdir 'C:UsersAniket' and command not found: create-react-app." Can someone please help me figure out

When attempting to install a React app, I used the following command: $ npx create-react-app my-app However, I encountered the following error: Error: EPERM: operation not permitted, mkdir 'C:\Users\Aniket' command not found: create-re ...

Is it possible to incorporate Material-UI Lab into my project without having to install the Core components as well?

I'm hoping to incorporate the Slider component from Material UI into my React application. The Slider is one of the components housed in the 'Lab' package, which acts as an incubator for features that aren't quite ready for the core. n ...