Node.js: ERR_DLOPEN_FAILED invalid ELF header
Invalid ELF header means a native addon was built for another OS or architecture. Rebuild it inside the target image instead of copying node_modules.

Introduction & Quick Summary
A service that worked on a laptop dies at boot in CI or in a container:
Error: dlopen(/app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node, 0x0001): invalid ELF header
at Object.Module._extensions..node (node:internal/modules/cjs/loader:1441:18)
code: 'ERR_DLOPEN_FAILED'Quick answer: a native addon in node_modules was compiled for a different operating system, CPU architecture, or C library than the one now running it. Rebuild the native modules inside the environment that will run them, and make sure the dependencies are installed there rather than copied in:
rm -rf node_modules
npm ci
npm rebuild --build-from-source# the reliable shape: install and build inside the same image
FROM node:22-bookworm-slim AS deps
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends python3 make g++ \
&& rm -rf /var/lib/apt/lists/*
COPY package*.json ./
RUN npm ci
FROM node:22-bookworm-slim
WORKDIR /app
COPY /app/node_modules ./node_modules
COPY . .
CMD ["node", "server.js"]invalid ELF header is the loader's way of saying the file is not a Linux shared object at all — usually because it is a macOS or Windows build that travelled with the repository, a Docker volume, or a CI cache. The same code family covers wrong ELF class, version GLIBC_2.28 not found, and cannot open shared object file, which have different fixes; this guide separates them.
Core Mechanics, Signatures & Reproducible Setup
Native addons are .node files: shared libraries that Node loads with process.dlopen(). The platform's dynamic loader reads the file header and expects a format it understands.
| File begins with | Format | Runs on |
|---|---|---|
7f 45 4c 46 (\x7fELF) | ELF | Linux, Android |
cf fa ed fe / ce fa ed fe | Mach-O | macOS |
4d 5a (MZ) | PE/COFF | Windows |
Installing a package that ships prebuilt binaries writes the file for the installer's platform. npm ci on a Mac in a bind-mounted folder, then docker run with that folder mounted, gives Linux a Mach-O file — the loader rejects it before any of your code runs.
A reproduction without Docker:
mkdir native-repro && cd native-repro
npm init -y
npm install bcrypt
xxd -l 4 node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node
# 00000000: cffa edfe ... <- Mach-O on macOS, 7f45 4c46 on Linux
node -e "require('bcrypt')" # succeeds here, fails after copying to the other OSThe reference for every variant is Node's error list under ERR_DLOPEN_FAILED, which reports the underlying dlerror string in the message. Read that string first; it decides the fix:
invalid ELF header— wrong file format (other OS).wrong ELF class: ELFCLASS64— 32-bit binary on a 64-bit Node, or the reverse.cannot open shared object file: No such file or directory— a system library the addon links against is missing.version 'GLIBC_2.28' not found— built on a newer distro than the one running it.undefined symbol: ...— ABI mismatch between the addon and the Node version or a shared dependency.
Key Comparison Table
| Message fragment | Cause | Fix |
|---|---|---|
invalid ELF header | binary built for macOS/Windows | rebuild inside Linux |
wrong ELF class | CPU word size mismatch | install the right arch |
GLIBC_2.28 not found | newer build host than runtime | build on the oldest supported base |
cannot open shared object file | missing system library | install the package that provides it |
Module did not self-register | ABI change in Node | rebuild against current Node |
NODE_MODULE_VERSION mismatch | Node major upgrade | npm rebuild |
Where node_modules came from | Runtime | Result |
|---|---|---|
| macOS laptop | Linux container, same folder | invalid ELF header |
| Debian glibc image | Alpine musl image | not found / ABI errors |
| x64 CI runner | arm64 production host | wrong ELF class |
| CI cache restored across Node majors | new Node | NODE_MODULE_VERSION mismatch |
| built inside the target image | same image | works |
| Package | Ships prebuilds | Needs build tools when absent |
|---|---|---|
bcrypt | yes, per platform | python3 make g++ |
sharp | yes, libvips bundled | usually none |
better-sqlite3 | yes | python3 make g++ |
node-canvas | no | cairo, pango, jpeg, gif libs |
grpc | yes | python3 make g++ |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Find which module is guilty
The stack names the .node path on the first line, but a lazy require can hide it behind another failure. Walk the tree before changing anything:
# every native binary in the dependency tree
find node_modules -name "*.node" -exec sh -c 'printf "%s " "$1"; xxd -l 4 -p "$1"' _ {} \;
# and the ones actually loaded by the failing entry point
node -e "require('./server.js')" 2>&1 | head -20If the header is cffaedfe or 4d5a, the file came from another machine and only that subtree needs rebuilding.
2. Install and build inside the target image
Multi-stage builds fail when a stage copies node_modules from a host install. The rule is that install, build, and run share one base image, so glibc and architecture match:
FROM node:22-alpine AS base
RUN apk add --no-cache python3 make g++
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/server.js"]Alpine's musl libc is the most common source of "worked on Debian, not on Alpine". Packages that publish prebuilt glibc binaries have no prebuild for musl, so they compile from source and need python3 make g++ in the image; adding them to the build stage is cheaper than switching base images late in a project.
3. Keep build tools out of the runtime image
Compile in one stage, run in another, both with the same libc family. Only the compiled node_modules and production dependencies cross the boundary:
FROM node:22-bookworm AS build
RUN apt-get update && apt-get install -y python3 make g++ && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev
FROM node:22-bookworm-slim
WORKDIR /app
COPY /app/node_modules ./node_modules
COPY /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]npm prune --omit=dev after the build keeps compilers out of the final layer while leaving the compiled binaries intact.
4. Apple Silicon, emulation, and platform flags
On an M-series Mac, installing under Rosetta or with a --platform=linux/amd64 container produces arm64 and x64 trees in different places. Pin the platform for both the install and the run:
docker buildx build --platform linux/amd64 -t app:amd64 .
docker run --platform linux/amd64 app:amd64 node -e "require('bcrypt'); console.log('ok')"Do not copy node_modules between an arm64 host and an x64 container. Reinstall with npm ci --cpu=x64 --os=linux inside the target, or let the build stage do it.
5. Lockfiles, optional dependencies, and cross-platform CI
package-lock.json records os and cpu for optional platform packages such as sharp or esbuild. A lockfile generated on one platform is still usable elsewhere, but a node_modules folder is not. In CI, key the cache on the OS, architecture, and Node major version so a poisoned cache cannot be restored by the wrong job:
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('package-lock.json') }}Cache the package manager's download directory, not node_modules; installing from cache is fast, and rebuilding native code at install time is what keeps the binaries correct.
6. Confirm the fix at the binary level
After rebuilding, verify the file format and the loaded module rather than trusting a green build:
xxd -l 4 node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node
# 00000000: 7f45 4c46 <- ELF, correct for Linux
node -e "console.log(require('bcrypt').hashSync('x', 4))"
ldd node_modules/better-sqlite3/build/Release/better_sqlite3.node | grep "not found" || echo "deps ok"ldd prints the shared libraries the binary expects; any not found line is the missing system package, and installing it is the fix rather than another rebuild. When you compare two Dockerfiles to see what changed between a working and a failing image, the text diff tool makes the stage difference visible, and the Docker Compose reference keeps the runtime service definitions consistent with the image you built.
Common Compilation/Runtime Mistakes & Version Gotchas
- Copying
node_modulesbetween machines. The single largest cause ofinvalid ELF header; copypackage*.jsonand install instead. - Restoring a
node_modulescache in CI. Fast, and it silently carries another platform's binaries; cache the npm download folder. - Adding build tools only to the runtime image. That inflates the final image and still fails if the base libc differs from the build host's.
- Assuming
npm rebuildfixes everything. It rebuilds modules whose install scripts support it; a pure prebuild-only package needs a reinstall so the right optional binary is fetched. - Upgrading Node without rebuilding. A major version bump changes
NODE_MODULE_VERSION;npm rebuildafter the upgrade, or reinstall from the lockfile. - Version notes. Node 22 and 23 report the loader failure as
ERR_DLOPEN_FAILEDwith thedlerrortext; older releases wrapped it inError: Module did not self-registerwith a less specific message. Node's built-in test runner andnode:sqlitereduce how often a third-party native addon is needed at all.
Official documentation: ERR_DLOPEN_FAILED in Node.js errors and npm rebuild.
Official references
primary documentationThe Node.js documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Node.js work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Node.js guides
All Node.js guidesNode.js ERR_INVALID_ARG_TYPE: the argument must be of type
ERR_INVALID_ARG_TYPE is Node's validator naming a wrong argument. Read the quoted argument, validate at the boundary and default env values.
ERR_MODULE_NOT_FOUND: Cannot find module imported from an ESM file
ESM resolves specifiers literally: extensions are required, directories do not resolve to index files, and exports maps block deep imports.
Node.js 26 upgrade: rmdir, native addons and removed flags
Three breaks stop a deploy: rmdir with recursive throws, the transform-types flag is gone, and the module ABI moved to 147.
Node.js JWT invalid signature: JWKS Rotation and Clock Skew
One message covers three bugs: the wrong key type for RS256 or ES256, a JWKS cache that never refreshes after a kid rotation, and a clock that drifted.
More in Node.js
All Node.js guidesThe guides filed next to this one in Node.js, in library order.