본문 바로가기
NodeJS

Error: Cannot find module @rollup/rollup-linux-x64-musl. npm has a bug related to optional dependencies

by TheWhisperOfLeaves 2025. 5. 20.

도커를 사용하여 개발 환경을 세팅하던 중 다음 문제가 발생하였다.

도커 환경 구성은 다음과 같다.

이미지: node:22.14.0-alpine

설치 패키지: vite + react-ts 템플릿

 

Dockerfile

FROM node:22.14.0-alpine AS base

RUN apk add --no-cache tzdata
RUN cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime

WORKDIR /app
COPY package.json package-lock.json ./

FROM base AS development
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]

 

docker-compose.dev.yml

services:
  mysql:
    image: mysql:8.0.39
    container_name: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test
      MYSQL_USER: dev
      MYSQL_PASSWORD: dev
    ports:
      - "3306:3306"
    volumes:
      - ./mysql:/var/lib/mysql
  frontend:
    restart: always
    build:
      context: ./frontend
      target: development
      dockerfile: Dockerfile
    container_name: frontend
    ports:
      - "3000:3000"
    volumes:
      - ./frontend:/app

 

compose를 사용하여 개발 환경을 실행하니 다음과 같은 문제가 발생했다.

Error: Cannot find module @rollup/rollup-linux-x64-musl. npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). Please try `npm i` again after removing both package-lock.j
son and node_modules directory.
    at requireWithFriendlyError (/app/node_modules/rollup/dist/native.js:64:9)
    at Object.<anonymous> (/app/node_modules/rollup/dist/native.js:73:76)
    at Module._compile (node:internal/modules/cjs/loader:1554:14)
    at Object..js (node:internal/modules/cjs/loader:1706:10)
    at Module.load (node:internal/modules/cjs/loader:1289:32)
    at Function._load (node:internal/modules/cjs/loader:1108:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
    at cjsLoader (node:internal/modules/esm/translators:262:5)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:196:7) {
  [cause]: Error: Cannot find module '@rollup/rollup-linux-x64-musl'
  Require stack:
  - /app/node_modules/rollup/dist/native.js
      at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)
      at Function._load (node:internal/modules/cjs/loader:1055:27)
      at TracingChannel.traceSync (node:diagnostics_channel:322:14)
      at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
      at Module.require (node:internal/modules/cjs/loader:1311:12)
      at require (node:internal/modules/helpers:136:16)
      at requireWithFriendlyError (/app/node_modules/rollup/dist/native.js:46:10)
      at Object.<anonymous> (/app/node_modules/rollup/dist/native.js:73:76)
      at Module._compile (node:internal/modules/cjs/loader:1554:14)
      at Object..js (node:internal/modules/cjs/loader:1706:10) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ '/app/node_modules/rollup/dist/native.js' ]
  }
}

 

스택오버플로우에 올라온 답변들 중 해결되었다는 답변들 몇 가지는 아래와 같았다.

1. node_modules와 package-lock.json 삭제 후 컨테이너 실행

2. .dockerignore에 node_modules 추가

 

위 2가지 방법으론 해결이 안되었다. 내가 해결한 방법은 아래와 같다.

node_modules를 호스트에서 마운트하지 않고, 컨테이너 내에서 설치하는 부분을 추가해줬다.

yml 파일을 아래와 같이 수정했다.

services:
  frontend:
    restart: always
    platform: linux/amd64 // 추가된 부분
    build:
      context: ./frontend
      target: development
      dockerfile: Dockerfile
    container_name: frontend
    ports:
      - "3000:3000"
    volumes:
      - ./frontend:/app
      - /app/node_modules // 추가된 부분

 

볼륨에 /app/node_modules만 추가해도 해결이 된다. platform은 해결 과정 중에 아키텍처 문제로 이런 경우가 발생할 수 있다고 한다. 특히나 alpine의 경우엔 c 라이브러리 의존성 문제도 있기 때문에 이런 경우가 발생할 수 있지 않을까 했던거고 나는 mbp m1을 사용하여서 ARM 아키텍처이므로 platform을 지정하였다.

댓글