Docker run fails 'return process.dlopen(module, path.toNamespacedPath(filename));'

Matthew C.
—The following error is returned in your terminal when you use the docker run command to create and run a new container from a Docker image of a JavaScript project:
Error: /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header at Module._extensions..node (node:internal/modules/cjs/loader:1454:18)
This error indicates that Node modules from a JavaScript project in a Windows or macOS environment have been added to a Docker image that is used to create Docker containers that run in a Linux environment.
It is best not to copy Node modules from a JavaScript project to a Docker image. This prevents cross-platform issues that can be caused by specific Node modules that have a host OS-specific or architecture-specific output that doesn’t work in a Linux environment.
Rather install the Node modules directly in the Docker image. You can use the official Node Docker image as a base image for your Node project, as it has Node.js installed.
To stop running and remove the Docker container causing the error, you will need its CONTAINER ID.
Run the list containers command to retrieve the CONTAINER ID:
docker ps
Then, run the stop command to stop the container:
docker stop <CONTAINER ID>
Run the rm command to remove the container:
docker rm <CONTAINER ID>
When you rebuild your Docker image, add the following RUN build command to your Dockerfile to install your Node modules in your Docker image:
RUN npm install
When you run the docker build command, you may see that the npm install step is cached.
=> CACHED [4/5] RUN npm install
Build caching is used to improve build speeds. You will need to disable the caching, so that the correct Linux-compatible bcrypt Node module is installed. Rerun the docker build command and include the --no-cache flag:
docker build --no-cache -t nodedemo:1.0.0 .
Rerunning the docker build command will generate a new Docker image new ID. You will need to use this new image ID when running the docker run command.
Find the updated Docker image ID using the Docker Desktop app or by running the following command to list all your Docker images:
docker image ls
If you need to include Node modules from Windows or macOS environments in your Docker image, you can prevent the error by creating a .dockerignore file to house the Node modules folder:
node_modules
If your Dockerfile uses the COPY command to copy your project’s files and folders to your Docker image, the .dockerignore file will prevent the node modules from being copied to the Docker image when the docker build command is run.
Note that Node modules copied into a Docker image will overwrite pre-existing Node modules in that Docker image.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
