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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With