Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache NodeJS global modules AWS CodeBuild

Is there a way to cache NodeJS global modules on AWS CodeBuild?

I'm using LernaJS to handle my repository and every time build starts I install it with the command npm install -g lerna (it takes 30 seconds).

To handle this, first I figured out where npm install Lerna with the command npm list -g and was returned

/usr/local/lib 
├─┬ [email protected] 
│ ├── [email protected] 
...
├─┬ [email protected] 
│ ├─┬ @lerna/[email protected] 
│ │ ├── @lerna/[email protected] deduped 
...

Then I tried to cache /usr/local/lib/node_modules/**/* folder and I received the following error:

[Container] 2019/05/30 20:09:00 Running command npm install -g lerna 
/codebuild/output/tmp/script.sh: 4: /codebuild/output/tmp/script.sh: npm: not found 

[Container] 2019/05/30 20:09:00 Command did not exit successfully npm install -g lerna exit status 127 
[Container] 2019/05/30 20:09:00 Phase complete: INSTALL State: FAILED 
[Container] 2019/05/30 20:09:00 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm install -g lerna. Reason: exit status 127 

So I checked the content of /usr/local/lib/node_modules/ I had these packages:

[Container] 2019/05/30 20:19:11 Running command ls /usr/local/lib/node_modules 
grunt 
grunt-cli 
lerna 
npm 
webpack 

My last attempt was cache /usr/local/lib/node_modules/lerna/**/*. This way no error is thrown, but cache doesn't work either:

[Container] 2019/05/30 20:30:00 MkdirAll: /codebuild/local-cache/custom/656f09faf2819a785eae5e09f5d26a44ff4f20edf155297d6819c9600540cd26/usr/local/lib/node_modules/lerna 
[Container] 2019/05/30 20:30:00 Symlinking: /usr/local/lib/node_modules/lerna => /codebuild/local-cache/custom/656f09faf2819a785eae5e09f5d26a44ff4f20edf155297d6819c9600540cd26/usr/local/lib/node_modules/lerna 

...

[Container] 2019/05/30 20:30:01 Running command npm install -g lerna 
/usr/local/bin/lerna -> /usr/local/lib/node_modules/lerna/cli.js 
+ [email protected] 
added 650 packages from 321 contributors and updated 1 package in 40.628s 

Am I missing something? Is there a way to save Lerna as grunt, grunt-cl, npm and webpack (inside /usr/local/lib/node_modules/) before building starts?

Thank you!

like image 505
Pedro Arantes Avatar asked Jul 28 '26 09:07

Pedro Arantes


1 Answers

Thanks to @JD D comment, I've created a docker image, pushed it to AWS ECR and use it as my own image.

My Dockerfile:

FROM node:lts
RUN npm install -g yarn lerna
RUN apt-get update && \
  apt-get install -y groff less && \
  apt-get clean
RUN curl https://s3.amazonaws.com/aws-cli/awscli-bundle.zip -o awscli-bundle.zip
RUN unzip awscli-bundle.zip  && \
  ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws && \
  rm awscli-bundle.zip
like image 159
Pedro Arantes Avatar answered Jul 29 '26 21:07

Pedro Arantes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!