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."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Node.js apps: npm & package.json

More on Node.js builds (npm, package.json)