Error: Cannot find module @rollup/rollup-linux-x64-musl on docker container
Asked Answered
D

3

5

I've set up a docker configuration to run my React app, but when starting the container, I encounter the following error:

frontend-1  | failed to load config from /app/vite.config.ts
frontend-1  | error when starting dev server:
frontend-1  | 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.json and node_modules directory.
frontend-1  |     at requireWithFriendlyError (/app/node_modules/rollup/dist/native.js:59:9)
frontend-1  |     at Object.<anonymous> (/app/node_modules/rollup/dist/native.js:68:76)
frontend-1  |     at Module._compile (node:internal/modules/cjs/loader:1368:14)
frontend-1  |     at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
frontend-1  |     at Module.load (node:internal/modules/cjs/loader:1205:32)
frontend-1  |     at Module._load (node:internal/modules/cjs/loader:1021:12)
frontend-1  |     at cjsLoader (node:internal/modules/esm/translators:366:17)
frontend-1  |     at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:315:7)
frontend-1  |     at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
frontend-1  |     at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
frontend-1 exited with code 1

The React app functions correctly when run locally outside of Docker, and it also operates as expected when launched using the docker run command. But, when attempting to start the app with docker-compose up, it fails to function. I was unable to find a solution online.

Dockerfile:

FROM node:21-alpine

RUN npm install -g vite

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 5173

CMD ["vite", "--host", "0.0.0.0"]

docker-compose.yml:

version: '3.8'

services:
  reactApp:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/app
    ports:
      - "9000:5173"

.dockerignore:

node_modules
package-lock.json
Duroc answered 16/4, 2024 at 18:35 Comment(2)
The volumes: block is overwriting the code and node_modules tree in your image with content from your host. Does deleting that block help?Rhodesia
Possible related: github.com/npm/cli/issues/4828Suggestive
A
7

I encountered the same issue when trying to dockerize a react app using vite.

I managed to solve the issue by adding a named volume to the docker-compose.yml file, like this:

dashboard:
    container_name: dashboard
    image: dashboard
    depends_on:
      - postgres
    build:
      context: ../../packages/dashboard
      dockerfile: Dockerfile
    ports:
      - "5173:5173"
    volumes:
      - ../../packages/dashboard:/app
      - node_modules:/app/node_modules
volumes:
  pgdata: {}
  node_modules: {}

To be completely honest, I don't know why it helped. I queried Perplexity (the LLM tool) and it suggested that it has something to do with ARM64 processors (I'm using M2 Pro Mackbook Pro).

Perplexity's explanation goes like this:

The root cause seems to be that when you mount your local node_modules directory into the Docker container using the volumes section, it tries to use the binaries from your local machine, which are likely compiled for a different architecture (e.g., x86_64) and are incompatible with the ARM64 architecture inside the container.

One of its references was this GitHub discussions about the same problem.

Asymmetry answered 13/5, 2024 at 21:26 Comment(4)
Thanks! This is the only thing that worked for my Vite project in an npm workspaceCassareep
Had the same problem trying to run Angular, this did it, thanksCrookes
Creating the named volume for node_modules, worked for vite / npm / react inside docker containerBaghdad
Indeed, the named volume fixes it because it will install the dependencies based on the host OS running. Nice trick, in my case I had the issue without having a dockerfile and instead an anoymous volume, with the whole app code on it (including node_module). Thus, lead of course to installing the dependencies on my host OS, but then the node:20 image specified on the docker compose won't be able to match the dependencies.Mcreynolds
J
2

Add this in your package.json, it works

  {
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
  "optionalDependencies": {
    "@rollup/rollup-linux-x64-musl": "4.9.5"
  }
}
    

I have written article on this: https://sandipdulal.medium.com/fixing-vite-build-error-on-linux-and-windows-using-docker-error-cannot-find-module-e73bb2fb479d

Jalap answered 17/9, 2024 at 12:0 Comment(0)
R
1

Problem is if you copy the package-lock.json, but if you omit and only copy package.json and the RUN npm install it will install libs according to the OS chosen in the dockerfile.

Volume does not explain it, but this makes more sense as to why

Redhot answered 9/8, 2024 at 10:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.