What is node_modules and why should it never be committed or copied into images?
BeginnerConceptNode.js builds (npm, package.json)
Answer
Short answer
node_modules is the folder where npm installs all packages — often hundreds of MB and thousands of files. It's generated from package.json + the lockfile, so committing it just bloats the repo. Copying your local one into an image can break things because native modules are compiled for your OS (macOS) and won't run on Linux.
What to do instead
# .gitignore
node_modules/
# .dockerignore
node_modules
Then always install inside the build: RUN npm ci.
Example of the native-module trap
Packages like bcrypt, sharp or sqlite3 contain compiled C++ code. Built on a Mac (darwin-arm64) and copied into a Linux (linux-x64) container → Error: ... invalid ELF header.
What to say in the interview
"node_modules is a build output of the lockfile. It's gitignored and dockerignored; the image always runs npm ci itself so native modules are compiled for the container's platform."
More on Node.js builds (npm, package.json)
What is package.json and what does it contain?BeginnerHow do you build and run a Node.js application?BeginnerWhat is the difference between npm install and npm ci?BeginnerWhy must package-lock.json be committed to Git?BeginnerWhat is the difference between dependencies and devDependencies?BeginnerWhat do the ^ and ~ symbols mean in package.json versions?Beginner