N
Nuxt2w ago
Viridian

Cannot find 'nuxt.mjs' during build in Dockerfile

Hey all, I'm trying to build my Nuxt application via Docker, but my Dockerfile is giving me the following error:
0.472 > nuxt-app@ build /app
0.472 > nuxt build
0.472
0.489 node:internal/modules/cjs/loader:1228
0.489 throw err;
0.489 ^
0.489
0.489 Error: Cannot find module '/app/node_modules/nuxt/bin/nuxt.mjs'
0.489 at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)
0.489 at Function._load (node:internal/modules/cjs/loader:1055:27)
0.489 at TracingChannel.traceSync (node:diagnostics_channel:322:14)
0.489 at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
0.489 at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
0.489 at node:internal/main/run_main_module:36:49 {
0.489 code: 'MODULE_NOT_FOUND',
0.489 requireStack: []
0.489 }
0.489
0.489 Node.js v22.14.0
0.499  ELIFECYCLE  Command failed with exit code 1.
0.472 > nuxt-app@ build /app
0.472 > nuxt build
0.472
0.489 node:internal/modules/cjs/loader:1228
0.489 throw err;
0.489 ^
0.489
0.489 Error: Cannot find module '/app/node_modules/nuxt/bin/nuxt.mjs'
0.489 at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)
0.489 at Function._load (node:internal/modules/cjs/loader:1055:27)
0.489 at TracingChannel.traceSync (node:diagnostics_channel:322:14)
0.489 at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
0.489 at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
0.489 at node:internal/main/run_main_module:36:49 {
0.489 code: 'MODULE_NOT_FOUND',
0.489 requireStack: []
0.489 }
0.489
0.489 Node.js v22.14.0
0.499  ELIFECYCLE  Command failed with exit code 1.
This is my Dockerfile:
ARG NODE_VERSION=22.14.0

FROM node:${NODE_VERSION}-slim AS build

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

WORKDIR /app

COPY ./package.json /app/
COPY ./pnpm-lock.yaml /app/

RUN pnpm install --shamefully-hoist

COPY . ./

RUN pnpm run build

FROM node:${NODE_VERSION}-slim

WORKDIR /app

COPY --from=build /app/.output ./

ENV HOST=0.0.0.0 NODE_ENV=production
ENV NODE_ENV=production

EXPOSE 3000

CMD ["node", "/app/server/index.mjs"]
ARG NODE_VERSION=22.14.0

FROM node:${NODE_VERSION}-slim AS build

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

WORKDIR /app

COPY ./package.json /app/
COPY ./pnpm-lock.yaml /app/

RUN pnpm install --shamefully-hoist

COPY . ./

RUN pnpm run build

FROM node:${NODE_VERSION}-slim

WORKDIR /app

COPY --from=build /app/.output ./

ENV HOST=0.0.0.0 NODE_ENV=production
ENV NODE_ENV=production

EXPOSE 3000

CMD ["node", "/app/server/index.mjs"]
Building locally seems to work just fine, so I'm not sure what's causing this issue. Any help would be appreciated!
2 Replies
kapa.ai
kapa.ai2w ago
Hi @Viridian. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the error message and your Dockerfile, I can see you're encountering a common issue when building Nuxt applications in Docker. The error indicates that Docker can't find the nuxt.mjs file during the build process. Looking at the knowledge sources, there are a few potential solutions: Missing .dockerignore file The most likely cause is that you're missing a .dockerignore file, which is causing your local node_modules folder to be copied over and interfering with the Docker build process. This exact issue was mentioned in a Stack Overflow post where the solution was adding a proper .dockerignore file Stack Overflow Post > Docker Build Problem "Error: Cannot find module". Create a .dockerignore file in the same directory as your Dockerfile with the following content:
node_modules
npm-debug.log
.nuxt
.output
node_modules
npm-debug.log
.nuxt
.output
Other potential solutions If adding the .dockerignore file doesn't solve your issue, consider these alternatives: 1. Use shamefully-hoist=true: In one of the knowledge sources, it was mentioned that some Nuxt modules might not work without the shamefully-hoist flag. You're already using this in your Dockerfile, which is good. 2. Try a different package manager: Some users reported success by switching from one package manager to another. If pnpm continues to cause issues, you might try npm or yarn. 3. Use buildx for cross-platform builds: If you're building for a different platform than your host, using docker buildx build instead of docker build might help, as mentioned in Stack Overflow Post > Nuxt build hanging when building in linux docker image. 4. Check your Nuxt version compatibility: Make sure your Nuxt version is compatible with the Node.js version you're using in your Dockerfile. Let me know if adding the .dockerignore file resolves your issue! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@173158033742888961> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?