# Docker Invalid ELF Header Error with Node Modules Fix

> Fix invalid ELF header errors in Docker by installing Node modules inside the Linux container instead of copying them from a Windows or macOS host

**URL:** https://sentry.io/answers/docker-run-fails-return-process-dlopen-module-path-tonamespacedpath-filename/

---

## The Problem

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.

## The Solution

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](https://hub.docker.com/_/node) 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:

```sh
docker ps
```

Then, run the `stop` command to stop the container:

```sh
docker stop <CONTAINER ID>
```

Run the `rm` command to remove the container:

```sh
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](https://docs.docker.com/build/cache/).

```sh
 => 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:

```sh
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:

```sh
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.

---

*Source: [sentry.io/answers/docker-run-fails-return-process-dlopen-module-path-tonamespacedpath-filename/](https://sentry.io/answers/docker-run-fails-return-process-dlopen-module-path-tonamespacedpath-filename/)*
