A Node.js app works locally but fails in the container with "Cannot find module". How do you troubleshoot?
IntermediateTroubleshootingNode.js builds (npm, package.json)
Answer
Short answer
The module isn't in the container's node_modules, or the code path is wrong. Common causes: the package is in devDependencies but production install skipped them, node_modules wasn't copied between Docker stages, the build output (dist/) wasn't copied, or a case-sensitive filename differs (macOS ignores case, Linux doesn't).
Steps
docker run -it --entrypoint sh shop-api:2.3.1
ls node_modules | grep express # is the package there?
ls dist/ # is the compiled code there?
cat package.json | grep -A20 '"dependencies"'
Checklist
| Cause | Fix |
|---|---|
| Package in devDependencies | Move it to dependencies |
npm ci --omit=dev but build tool needed at runtime |
Build in an earlier stage |
| node_modules not copied to final stage | COPY --from=build /app/node_modules ./node_modules |
Imports ./Utils but file is utils.js |
Fix case — Linux is case-sensitive |
| Native module built for macOS copied in | Never copy local node_modules — add it to .dockerignore |
Wrong start path (server.js vs dist/server.js) |
Fix CMD / start script |
What to say in the interview
"I shell into the image and check node_modules and dist. Usually it's a runtime package in devDependencies, a missed COPY between stages, or a case-sensitivity issue, since Linux filesystems are case-sensitive and macOS usually isn't."
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