Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readNode.js
#nodejs#native-modules#docker#dlopen
Node.js native module crate with a mismatched platform binary sliding out of the stack

Introduction & Quick Summary

A service that worked on a laptop dies at boot in CI or in a container:

code
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:

bash
rm -rf node_modules npm ci npm rebuild --build-from-source
dockerfile
# 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 --from=deps /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 withFormatRuns on
7f 45 4c 46 (\x7fELF)ELFLinux, Android
cf fa ed fe / ce fa ed feMach-OmacOS
4d 5a (MZ)PE/COFFWindows

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:

bash
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 OS

The 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 fragmentCauseFix
invalid ELF headerbinary built for macOS/Windowsrebuild inside Linux
wrong ELF classCPU word size mismatchinstall the right arch
GLIBC_2.28 not foundnewer build host than runtimebuild on the oldest supported base
cannot open shared object filemissing system libraryinstall the package that provides it
Module did not self-registerABI change in Noderebuild against current Node
NODE_MODULE_VERSION mismatchNode major upgradenpm rebuild
Where node_modules came fromRuntimeResult
macOS laptopLinux container, same folderinvalid ELF header
Debian glibc imageAlpine musl imagenot found / ABI errors
x64 CI runnerarm64 production hostwrong ELF class
CI cache restored across Node majorsnew NodeNODE_MODULE_VERSION mismatch
built inside the target imagesame imageworks
PackageShips prebuildsNeeds build tools when absent
bcryptyes, per platformpython3 make g++
sharpyes, libvips bundledusually none
better-sqlite3yespython3 make g++
node-canvasnocairo, pango, jpeg, gif libs
grpcyespython3 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:

bash
# 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 -20

If 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:

dockerfile
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:

dockerfile
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 --from=build /app/node_modules ./node_modules COPY --from=build /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:

bash
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_modules between machines. The single largest cause of invalid ELF header; copy package*.json and install instead.
  • Restoring a node_modules cache 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 rebuild fixes 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 rebuild after the upgrade, or reinstall from the lockfile.
  • Version notes. Node 22 and 23 report the loader failure as ERR_DLOPEN_FAILED with the dlerror text; older releases wrapped it in Error: Module did not self-register with a less specific message. Node's built-in test runner and node:sqlite reduce 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 documentation

The Node.js documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Node.js work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Node.js guides

More in Node.js

All Node.js guides

The guides filed next to this one in Node.js, in library order.