skip to Main Content

I have a problem with the execution of the docker build command, in my Nuxt frontend docker container. This is my Dockerfile:

FROM node:lts-alpine as base

RUN npm install -g pnpm

WORKDIR /usr/src/app

COPY package.json pnpm-lock.yaml ./

RUN pnpm install

COPY . .

RUN pnpm run build
EXPOSE 3000

FROM node:lts-alpine as final
WORKDIR /app
COPY --from=base /usr/src/app/.output .

EXPOSE 3000
CMD ["node", "server/index.mjs"]

When running docker build with this Dockerfile I get the following error:

 => [base 4/7] COPY package.json pnpm-lock.yaml ./                                                                 0.1s
 => [base 5/7] RUN pnpm install                                                                                   13.0s
 => [base 6/7] COPY . .                                                                                            2.7s
 => ERROR [base 7/7] RUN pnpm run build                                                                            0.7s
------
 > [base 7/7] RUN pnpm run build:
0.581
0.581 > @ build /usr/src/app
0.581 > nuxi build
0.581
0.602 node:internal/modules/cjs/loader:1147
0.602   throw err;
0.602   ^
0.602
0.602 Error: Cannot find module '/usr/src/app/node_modules/nuxt/bin/nuxt.mjs'
0.602     at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
0.602     at Module._load (node:internal/modules/cjs/loader:985:27)
0.602     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
0.602     at node:internal/main/run_main_module:28:49 {
0.602   code: 'MODULE_NOT_FOUND',
0.602   requireStack: []
0.602 }
0.602
0.602 Node.js v20.11.1
0.605  ELIFECYCLE  Command failed with exit code 1.
------
Dockerfile:24
--------------------
  22 |     COPY . .
  23 |
  24 | >>> RUN pnpm run build
  25 |     EXPOSE 3000
  26 |
--------------------
ERROR: failed to solve: process "/bin/sh -c pnpm run build" did not complete successfully: exit code: 1
  1. I ran docker build with the --no-cache flag.
  2. I commented out the RUN pnpm run build line and went inside the container to check if the file exists in there. It does not.

Should this file even be in there? Has anybody had the same problem?

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution. I was missing the .dockerignore file, so my node_modules folder was copied over.


  2. Do you have all of the required packages listed in package.json?

    🗎 package.json

    {
      "name": "test",
      "private": true,
      "scripts": {
        "build": "nuxi build"
      },
      "engines": {
        "npm": "^9.0.0"
      },
      "dependencies": {
        "nuxt": "3.11.1",
        "nuxi": "3.11.1",
        "@nuxt/kit": "3.11.1"
      }
    }
    

    🗎 Dockerfile (Same as the one provided in question except the EXPOSE omitted from base stage.)

    FROM node:lts-alpine as base
    
    RUN npm install -g pnpm
    
    WORKDIR /usr/src/app
    
    COPY package.json pnpm-lock.yaml ./
    
    RUN pnpm install
    
    COPY . .
    
    RUN pnpm run build
    
    FROM node:lts-alpine as final
    
    WORKDIR /app
    
    COPY --from=base /usr/src/app/.output .
    
    EXPOSE 3000
    
    CMD ["node", "server/index.mjs"]
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search