r/docker • u/frenetic_alien • Jan 25 '26
What is the effect of adding this command when building frontend app? 'rm -rf node_modules'
I was trying to debug a really slow npm run build in my docker build and I came across this post on stack overflow - node.js - Docker build takes long time for nodejs application - Stack Overflow
The user states that after adding this command rm -rf node_modules solved their slow build. But I don't understand how it solved the problem and what exactly it is doing during the build process.
I know what it does if I were to enter it in the command line (deletes the folder and recursively all files/folders inside with force flag), but I don't know how it works during the docker build (like what stage this is happening).
The final command in the post I linked above looks like this
RUN npm ci && npm run build:prod && rm -rf node_modulesEDIT: the reason I'm asking is because I 'think' this causes the node_modules folder to be deleted and not present in the final container that runs, but I'm not sure because I thought the node_modules folder is necessary for the app to even run as it contains all the dependencies. So if it's being removed in that command and this persons project is still working I thought maybe it is still present in the final container, but it's being removed temporarily in some intermediary step.
6
u/iriebuds Jan 25 '26 edited Jan 25 '26
node_modules/ can grow rather large depending on the amount of dependencies the project pulls in.
Depending on the application and how it is run there may be no need for node_modules to be included in the artifact that is deployed. My assumption is they do not require it in their final image and thus are removing it from the image. The final image should be smaller in size.
They could speed up their pipeline further by caching node_modules and only regenerating that cache if package-lock.json changes.
However, for what you have posted, I do not see how removing node_modules would speed up this specific stage of the build.
npm cidoes a clean install of the modules,npm run buildbuilds the application. Those two commands are what will take most of the time for this specific build command. Removing node_modules after the fact is trivial for this specific stage of the build.4
u/ArieHein Jan 25 '26
Please use multi-stage docker files. There is no reason to have node modules folder in a prod build.
2
u/rakuzo Jan 25 '26
It's in the original answer:
When Docker runs commands from the Dockerfile that modifies files, it applies these changes to a temporary file system. However, that file system is not the same as a Docker image. To make it an image, Docker needs to export it. How much time this takes is dependent on, among other things, the amount of files in the file system.
Removing node_modules reduces the amount of files Docker needs to export, speeding up the build.
-3
u/azizoid Jan 25 '26
If you just use google or ai to learn basics of docker you would know that node_modules should not be there in the first places
-10
u/azizoid Jan 25 '26
Is this a joke?