1

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
9
  • How are you running the container? You've included both Docker and Kubernetes-related tags; which container environment are you using? Of note there's a common setup for running Node-based applications that tries to store the library tree in a volume, but this isn't necessary and can lead to the sorts of problems you're reporting. It'd help if you could edit the question to include the package.json file (or at least the part that lists the semver dependency) and whatever docker run command or docker-compose.yml file or Kubernetes YAML manifest launches the container. Commented Dec 15 at 16:08
  • @DavidMaze I edited the post to include the package.json. I'll also look into what you mentioned about the volumes. Oh, and I'm running the container with Helm. I could include the YAML files for that if it'll help. I'm not sure there's useful information there tho. Commented Dec 15 at 16:34
  • Please update your post to show the full error, because that'll include a lot of details about where things are going wrong. Commented Dec 15 at 17:41
  • 1
    How you run it? Don't you mount something into /usr/src/app/node_modules? Commented Dec 15 at 20:03
  • 1
    I tried your Dockerfile locally, and the image was built with no errors. Also the stack trace of the error clearly says that 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. Commented Dec 16 at 14:36

2 Answers 2

0

The problem is that semver is a nested dependency, and you need to use:

npm install

in production.

Sign up to request clarification or add additional context in comments.

3 Comments

Not really, no. I already have npm install --only=production in production stage. But to make sure, I removed the --only=production flag to install all deps. Still, the error persists.
tried? npm install semver
Yes. Both in the package.json before it's copied, and also during the build process. Globally and locally. Neither seem to change anything.
0

Webpack bundled your app expecting semver to exist in node_modules at runtime, but npm i --only=production didn't install it.

Two fixes:

Add it directly:

"dependencies": {
  "semver": "^7.6.0"
}

Or just use npm i without the production flag:

RUN npm i

But honestly check your Helm chart first. If you're mounting a volume into /usr/src/app or /usr/src/app/node_modules, that would wipe out everything you installed during the build. That would also explain why your other apps work fine (different volume config).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.