

Why you should never use ‘npm install’ in your CI/CD pipelines
source link: https://medium.com/javascript-in-plain-english/why-you-should-never-use-npm-install-in-your-ci-cd-pipelines-da0b89346d8d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Responses (2)
You have 2 free member-only stories left this month.
Why you should never use ‘npm install’ in your CI/CD pipelines
When to use npm ci
and npm install
explained in very simple terms.
When starting a new project or cloning an existing one most people run npm install
or the shorthand version npm i
which is fine. But most likely you have already stumbled over a similar command called npm ci
.
Often I still see the npm install
command in regular CI/CD pipelines. What sounds fine at first can lead to quite some problems. The ci
in the npm ci
commands stand for “clean install” or if it helps you, you can also use “continuous integration”. So it's quite clear that it should be used in favor of npm install
in your pipelines. But let's quickly check what the differences are before you blindly follow my advice and use npm ci
in all of your pipelines.
Even though the following definitions aren’t 100% aligned with the official NPM documentation I try to explain them in super simple terms containing the most common use cases.
npm install
It’s being used for adding dependencies to your package.json
and package-lock.json
, thus modifying your dependencies and installing them on your machine. This is absolutely fine for development but it should for no reason ever happen in your pipelines. You don’t want to add/modify any files in your pipelines.
npm ci
This command relies solely on the dependency definitions, which are listed in the package.json
and package-lock.json
, and installs them. BUT it doesn’t do any modifications to your files. Also if the definitions in your two JSON files aren’t 100% aligned with each other this command throws an error which also means that a lock file has to exist. So make always sure that you add it to your VCS. Besides that, it does a “clean install”, like the name suggests, which means that any existing node_modules
folder is being deleted.
Okay, but why is this important?
Actually, it's pretty simple. Whenever you start your CI/CD pipelines you don’t want to have any inconsistency due to non-matching package*.json
files which might result in a different codebase than your local one. Also, you don’t want to have any side effects which could appear when you are using an already populated node_modules
folder.
Just imagine what would happen if one of your developers commits only the package.json
but not the package-lock.json
. The pipeline wouldn’t be sure which packages to use now if the JSON files are not aligned anymore. Also, the pipeline might suddenly use some packages the developer hasn’t on his local machine and thus the typical “but it works on my machine” might occur again.
What you want in CI/CD pipelines are strongly consistent, reproducible steps that are not affected by any side effects. So it should never matter how often you run a specific pipeline. When the same parameters (optional) are provided it should always produce the same result regardless if it is linting, building, testing, or even deploying your software.
Conclusion
I hope I could show you why it's so super important to use the npm ci
command in your pipelines and refer from using the “normal” npm install
.
So just to summarize:
- Use
npm install
when you are working on your local machine and want to install/add/modify dependencies. - Use
npm ci
when you are running on a CI/CD pipeline and want to provide consistency with reproducible steps.
Thanks for taking the time to read my article.
If you have any questions or additions feel free to use the comment section or hit me up on LinkedIn or Twitter to get in contact with me 😊
Recommend
-
19
Why You Should Never Use LAMP & WAMPP etc So, We a...
-
8
Dog Breeds You Should Never Handle On Your Own They are known as man’s best friends but just how true is this? Dogs are faithful and loyal friends when they are trained properly to heed the voice of...
-
8
Why You Should Never Use a Free VPN If You Value Your Privacy By Alexiei Zahorski Published 22 hours ago L...
-
5
Why You Should Never Let Your Android Battery Hit Zero
-
10
Why you should never use the browser in Facebook and Instagram News
-
7
Why you should never use px to set font-size in CSS Published: October 28,...
-
7
-
8
Why You Should Never Use WD-40 On Plastic
-
3
Next Up Cars Best Cars Of 2023 — 10 Of The Top Rated Picks ...
-
6
38 3 5 4 3 Why you sh...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK