diff --git a/README.md b/README.md index 2d87bcd6..23f01a5c 100644 --- a/README.md +++ b/README.md @@ -1081,13 +1081,14 @@ Bear in mind that with the introduction of the new V8 engine alongside the new E

-## ![✔] 8.1. Clean npm cache +## ![✔] 8.1. Clean NODE_MODULE cache -**TL;DR:** +**TL;DR:** After installing dependencies in a container, remove the local cache. It doesn't make any sense to duplicate the dependencies packages to speed future installs since there won't be any further installs - A docker image is created only once. By doing so, using a single line of code, tens of MB, typically 10-50% of the image size are shaved -**Otherwise:** -🔗 [**Read More: Clean npm cache**](/sections/docker/file.md) +**Otherwise:** The image that will get shipped to production will weigh 30% more due to files that will never get used + +🔗 [**Read More: Clean NODE_MODULE cache**](/sections/docker/clean-cache.md)


@@ -1320,9 +1321,9 @@ Thank you to all our collaborators! 🙏 Our collaborators are members who are contributing to the repository on a regular basis, through suggesting new best practices, triaging issues, reviewing pull requests and more. If you are interested in helping us guide thousands of people to craft better Node.js applications, please read our [contributor guidelines](/.operations/CONTRIBUTING.md) 🎉 -| | | | -| :---------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------: |:--------------------------------------------------------------------------------------------------------------------------------: | -| [Ido Richter (Founder)](https://github.com/idori) | [Keith Holliday](https://github.com/TheHollidayInn) | [Kevyn Bruyere](https://github.com/kevynb) | +| | | | +| :---------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------: | +| [Ido Richter (Founder)](https://github.com/idori) | [Keith Holliday](https://github.com/TheHollidayInn) | [Kevyn Bruyere](https://github.com/kevynb) | ### Past collaborators diff --git a/sections/docker/clean-cache.md b/sections/docker/clean-cache.md new file mode 100644 index 00000000..625ca40a --- /dev/null +++ b/sections/docker/clean-cache.md @@ -0,0 +1,25 @@ +# Clean NODE_MODULE cache + +

+ +### One Paragraph Explainer + +Node package managers, npm & Yarn, cache the installed packages locally so that future projects which need the same libraries won't need to fetch from a remote repository. Although this duplicates the packages and consumes more storage - it pays off in a local development environment that typically keeps installing the same packages. In a Docker container, this storage increase is worthless since it installs the dependency only once. By removing this cache, using a single line of code, tens of MB are shaved from the image. While doing so, ensure that it doesn't exit with non-zero code and fail the CI build because of caching issues - This can be avoided by including the --force flag + +

+ +### Code Example – Clean cache + +
+Dockerfile + +``` +FROM node:12-slim AS build +WORKDIR /usr/src/app +COPY package.json package-lock.json ./ +RUN npm ci --production && npm clean cache --force + +# The rest comes here +``` + +
\ No newline at end of file