Pipelines: cache custom node_modules folder

anurbol April 11, 2018

Do I understand correctly that pre-defined `node` cache only works when node_modules and package.json are located in the root directory of a project? Then how do I configure bitbucket-pipelines.yml to make cache work with node_modules folder located in subfolder (./public/node_modules/)? 

1 answer

1 accepted

6 votes
Answer accepted
anurbol April 11, 2018

Managed to solve it

 

with:

...
- step
caches:
- node-custom
script:
- if [ ! -d "node_modules" ]; then npm install; fi
...

definitions:
caches:
node-custom: public/node_modules
James Hyatt September 17, 2019

Will that not cause problems if package.json is updated as the node_modules won't be re-generated once cached?

Joshua Tonga October 17, 2019

It will. You need to manually delete your cached dependencies using the UI.

Petr Peller November 16, 2019

There sure must be a way how to delete it automatically when package-lock.json changes?

Thiago Silva February 28, 2020

@Petr Pelleractually, you don't need that line:

- if [ ! -d "node_modules" ]; then npm install; fi

Because the npm knows when to install/remove packages, I just changed this line by simply:

- npm install

And it works fine for me.

Like Roman likes this
Edgars Zagorskis January 19, 2022

I got on this problem while trying to set up a monorepo. And even more - i want to spare the execution of costly npm command. So I generated MD5 hash of the package-lock.json and then cache node_modules AND the MD5 hash. So far looks to be working fine. 

definitions:
caches:
app1: app1/node_modules
app1-cache-key: app1/package-lock.md5

pipelines:
branches:
dev:
- step:
script:
- cd app1
- package-lock.md5
- if [[ $(cat package-lock.md5 || '') != $(md5 package-lock.json) ]]; then npm ci && echo "$(md5 package-lock.json)" > package-lock.md5 ; fi

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events