I'm getting a Node.js error Cannot find module 'semver' when I run my Docker image with Helm.
I have a fairly standard Node.js+Nest.js project. The Dockerfile is as below:
FROM node:alpine AS development
WORKDIR /usr/src/app
COPY package.json ./
COPY package-lock.json ./
COPY tsconfig.json tsconfig.json
COPY nest-cli.json nest-cli.json
RUN npm i
COPY apps/auth apps/auth
COPY libs libs
RUN cd apps/auth && npm i
RUN npm run build auth
# ####################################################
FROM node:alpine AS production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package.json ./
COPY package-lock.json ./
RUN npm i --only=production
COPY --from=development /usr/src/app/dist ./dist
CMD [ "node", "dist/apps/auth/main" ]
It's confusing to me, beause I have multiple other apps running almost the same build steps (with the difference of the src code), but those are running just fine.
I have come across a similar question, but the solution was more for native Linux machines. Like I don't want to have to re-install Node.js when running the container, after I'd supposedly already done that building the image, if that makes sense.
Edit:
Here's the package.json as well. As you can see, I don't have semver as a direct dependency, but npm ls semver show that it's a nested dependency @nestjs/jwt -> jsonwebtoken -> semver. However, I think it's possible that Node or NPM have semver as a core dependency and that's where the issue is, not semver as an app dependency.
"dependencies": {
"@nestjs/jwt": "^11.0.1",
"@nestjs/passport": "^11.0.5",
"bcryptjs": "^3.0.3",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.6",
"@types/passport-jwt": "^4.0.1",
"@types/passport-local": "^1.0.38"
}
The full error message I'm getting from the pods:
node:internal/modules/cjs/loader:1423
throw err;
^
Error: Cannot find module 'semver'
Require stack:
- /usr/src/app/dist/apps/auth/main.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1420:15)
at defaultResolveImpl (node:internal/modules/cjs/loader:1058:19)
at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1063:22)
at Module._load (node:internal/modules/cjs/loader:1226:37)
at TracingChannel.traceSync (node:diagnostics_channel:328:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:245:24)
at Module.require (node:internal/modules/cjs/loader:1503:12)
at require (node:internal/modules/helpers:152:16)
at Object.<anonymous> (/usr/src/app/dist/apps/auth/main.js:1979:18)
at __webpack_require__ (/usr/src/app/dist/apps/auth/main.js:8687:42) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/usr/src/app/dist/apps/auth/main.js' ]
}
Node.js v25.2.1
package.jsonfile (or at least the part that lists thesemverdependency) and whateverdocker runcommand ordocker-compose.ymlfile or Kubernetes YAML manifest launches the container./usr/src/app/node_modules?requireStack: [ '/usr/src/app/dist/apps/auth/main.js' ]so the problem appears to be in run time, not build image time. Worth to check what is in node_modules when image is run.