Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

bcrypt_lib.node: invalid ELF header — why bcrypt fails in Docker and on Linux

invalid ELF header, with code ERR_DLOPEN_FAILED, means Node on Linux tried to load a bcrypt binary that is not a Linux library: node_modules was installed on a Mac or Windows and then copied to the server, by a COPY . . without a .dockerignore or a committed node_modules folder. Keep node_modules out of the image and the repository and install inside Linux with npm ci, or switch to bcryptjs, which has no binary at all.

By Livops team, the people who build and run LivopsUpdated

The error, as Node prints it on Linux when bcrypt’s native library is not a Linux file:

Error: /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header
    at Object..node (node:internal/modules/cjs/loader:2012:18)
    at Module.load (node:internal/modules/cjs/loader:1577:32)
    ...
  code: 'ERR_DLOPEN_FAILED'
}

The path changes with the bcrypt version and the folder the app runs in: lib/binding/napi-v3/bcrypt_lib.node is bcrypt 5[7]; with bcrypt 6 the file that fails is usually build/Release/bcrypt_lib.node (cause 6)[11][13]. The stack lines above are from Node 24. This page explains what the loader is telling you, the causes from the most common, the fix for each, and how bcrypt is installed on Livops.

What does the error mean?

Node asked the system loader, and the loader refused

bcrypt is a native addon: compiled C++ in a .node file that Node loads with process.dlopen(). ERR_DLOPEN_FAILED means that call failed[1], and on Linux the message is the system loader’s own error text, passed through unchanged[2]. glibc formats it as the file’s path, a colon, and the reason[4].

“invalid ELF header”: the file is not a Linux library

Linux libraries are ELF files. glibc checks the first bytes of the file, the ELF magic number, and when they do not match it reports invalid ELF header[3]. A macOS library (Mach-O) or a Windows one (a DLL) fails exactly that check. So the error almost always means the bcrypt binary in node_modules was built on a Mac or on Windows, and the folder was then carried to Linux.

The error shows What it means
invalid ELF header Not a Linux file at all: a macOS or Windows build, on glibc Linux (Debian, Ubuntu)[3]
Error loading shared library ...: Exec format error The same on Alpine, whose musl loader rejects a file that is not a loadable ELF with ENOEXEC[5], printed as Exec format error[6]
cannot open shared object file: No such file or directory A Linux file for another CPU, such as arm64 on an x64 server: glibc treats a library for another machine as not found[3]
wrong ELF class: ELFCLASS32 A 32-bit Linux file on a 64-bit system[3]

When the whole container fails with exec format error before Node even starts, the image itself is for another CPU; see exec format error in Docker.

Causes, most common first

1. COPY . . brings node_modules from your laptop

The Dockerfile installs dependencies, then copies the project folder over them, or copies the folder and never installs at all. Without a .dockerignore, the folder includes the node_modules npm made on your Mac, and the macOS bcrypt_lib.node ends up in the Linux image.

Fix: list node_modules in a .dockerignore at the root of the build context. Docker removes matching files and folders from the context before it is sent to the builder[16], so COPY . . can no longer overwrite what the image installed:

# .dockerignore
node_modules
npm-debug.log

Then install inside the image, before copying the code:

FROM node:22-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "server.js"]

2. node_modules is committed to the repository

The same thing, through git: the server clones a node_modules folder made on a developer’s laptop. Fix: remove it from the repository and keep it out:

git rm -r --cached node_modules
echo node_modules >> .gitignore
git commit -m "Stop committing node_modules"

3. node_modules was uploaded or synced to a server

A .zip with node_modules inside, or rsync of the whole project to a VPS. Fix: leave node_modules out of what you send and run npm ci on the server. If a node_modules folder is already there, npm ci removes it before it installs[14], so nothing from the laptop survives.

4. A multi-stage build installs on one machine and runs on another

A builder stage on one platform, a runtime stage on another, and COPY --from=build /app/node_modules between them. A stage on Alpine (musl) and one on Debian (glibc), or an arm64 stage and an x64 one, carry the wrong binary across. Fix: use the same base image, and the same platform, for the stage that installs dependencies and the stage that runs them.

5. npm rebuild did not replace the file

With bcrypt 5, the install script is node-pre-gyp install --fallback-to-build[7]. When bcrypt_lib.node already exists, node-pre-gyp leaves it alone and prints[8]:

[bcrypt] Success: "/app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node" already installed
Pass --update-binary to reinstall or --build-from-source to recompile

npm rebuild works by running the package’s install scripts again[15], so npm rebuild bcrypt on a copied folder can report success and keep the macOS file. --update-binary replaces the local binary with the downloaded one, and --build-from-source compiles it[9]. Simpler: delete node_modules and run npm ci on the Linux machine.

6. bcrypt 6 and a stale build/Release folder

bcrypt 6.0.0 replaced node-pre-gyp with prebuilt binaries shipped inside the package[10], loaded by node-gyp-build[11]. The 6.0.0 package carries builds for macOS (arm64, x64), Linux (x64, arm64 and arm, each for glibc and musl) and Windows (x64, arm64)[12], and node-gyp-build picks the one for the platform and CPU it runs on[13]. So a copied node_modules usually works with bcrypt 6.

The exception: node-gyp-build checks build/Release first and only then the prebuilt folder[13]. If bcrypt was ever compiled from source on your Mac, build/Release/bcrypt_lib.node is a macOS file, and it wins on Linux too. Fix: the same as cause 1 or 3, or upgrade to bcrypt 6 and reinstall on Linux. bcrypt 6 requires Node 18 or later[11].

Or switch to bcryptjs

bcryptjs is bcrypt written in pure JavaScript, compatible with the C++ binding[17], so there is no .node file to mismatch and nothing to compile. The trade-off is speed: its README says it is about 30% slower, which means fewer rounds in the same time[17].

npm uninstall bcrypt
npm install bcryptjs
import bcrypt from 'bcryptjs'

const hash = await bcrypt.hash(password, 10)
const ok = await bcrypt.compare(password, hash)

bcryptjs has the same hash and compare calls[17], and because it is compatible with the C++ binding, hashes you already stored keep working. If hashing time matters for your load, keep bcrypt and fix the install instead.

How to check the fix before you deploy

Look at the file the app loads, inside the image or on the server:

find node_modules/bcrypt -name '*.node' -exec sh -c 'printf "%s:" "$1"; od -An -c -N4 "$1"' _ {} \;

A Linux library starts with 177 E L F, the ELF magic number. Anything else, such as 317 372 355 376 for a macOS file, means the image still carries a laptop’s node_modules. Where the file command is installed, it names the platform too: ELF 64-bit LSB shared object, x86-64 or Mach-O 64-bit bundle arm64. Then load bcrypt the way the app does:

node -e "require('bcrypt'); console.log('bcrypt loads')"

If the install itself fails because bcrypt has to compile and the machine has no compiler, see node-gyp rebuild failed.

On Livops

  • Livops installs the dependencies itself. It detects the framework and picks the install command; you can change it under Settings → Install, or with install in livops.json. See Frameworks and livops.json.
  • An uploaded folder never brings node_modules. When you upload a folder or a .zip, node_modules is dropped automatically; see Ways to deploy.
  • A GitHub checkout skips node_modules too. When Livops fetches a repository, a committed node_modules folder is left out of the checkout, so causes 2 and 3 cannot reach the build.
  • bcrypt gets a compiler. When package.json lists bcrypt, Livops builds in an image that has the compiler node-gyp needs. If the build log still says gyp ERR! find Python, the docs’ answer is bcryptjs instead of bcrypt, or a Dockerfile; see Install failed.
  • Node 22 or 24. Those are the Node versions Livops has, and both meet bcrypt 6’s minimum. Livops reads .nvmrc and .node-version.
  • Your own Dockerfile. If the repository has a Dockerfile, it is used, and causes 1, 4 and 6 come from what it copies and which base images it uses. Install in the image with npm ci, as in cause 1.
  • Where the error shows. An app that loads bcrypt when it starts fails the deploy as start_failed; the error is on the Logs page, and a web app’s previous version keeps running. See The app did not start.

A Node app costs 39 credits a month (39,000 so’m); see Pricing.

The same kind of mismatch with another native module: Could not load the “sharp” module using the linux-x64 runtime.

Questions

What does invalid ELF header mean?

Linux libraries are ELF files, and every ELF file starts with the same four bytes. The Linux loader opened bcrypt_lib.node, did not find those bytes, and refused it. That happens when the file is a macOS or Windows library, which is what you get when node_modules is installed on a Mac or a Windows PC and copied to Linux.

Why does bcrypt work on my Mac but not in Docker?

On your Mac, npm installed a macOS build of bcrypt's native library. If the Dockerfile copies the project folder with that node_modules inside, the container gets the macOS file, and Linux cannot load it. Add node_modules to .dockerignore and run npm ci inside the image.

Will npm rebuild bcrypt fix it?

Not always. With bcrypt 5, the install script runs node-pre-gyp, which sees a bcrypt_lib.node already in place and prints already installed without replacing it. Delete node_modules and run npm ci on the Linux machine instead, which installs every package for that machine.

Should I switch to bcryptjs?

It removes the problem for good: bcryptjs is pure JavaScript, so there is no binary to mismatch, and its hashes are compatible with bcrypt. Its README says it is about 30 percent slower, which means fewer rounds in the same time. If you keep bcrypt, installing inside Linux fixes the error just as well.

What if the error says cannot open shared object file instead?

When the file is a Linux library built for another CPU, for example arm64 on an x64 server, glibc does not say invalid ELF header. It reports cannot open shared object file: No such file or directory, even though the file is there. The fix is the same: install on the machine, or in the image, that runs the app.

Does bcrypt 6 still have this problem?

Less often. bcrypt 6 ships prebuilt binaries for macOS, Linux and Windows inside the package and picks the one for the machine it runs on. But a build/Release folder compiled on your Mac is checked first, so a copied node_modules can still bring a macOS file with it.

Sources

  1. Node.js docs — Errors, ERR_DLOPEN_FAILED (nodejs.org). Checked .
  2. Node.js v24.16.0 source — src/node_binding.cc (github.com). Checked .
  3. glibc 2.41 source — elf/dl-load.c (sourceware.org). Checked .
  4. glibc 2.41 source — dlfcn/dlerror.c (sourceware.org). Checked .
  5. musl 1.2.5 source — ldso/dynlink.c (git.musl-libc.org). Checked .
  6. musl 1.2.5 source — src/errno/__strerror.h (git.musl-libc.org). Checked .
  7. bcrypt v5.1.1 source — package.json (github.com). Checked .
  8. node-pre-gyp v1.0.11 source — lib/install.js (github.com). Checked .
  9. node-pre-gyp v1.0.11 — README (github.com). Checked .
  10. bcrypt v6.0.0 — CHANGELOG (github.com). Checked .
  11. bcrypt v6.0.0 source — package.json (github.com). Checked .
  12. unpkg — [email protected] package files, prebuilds (unpkg.com). Checked .
  13. node-gyp-build v4.8.4 source — node-gyp-build.js (github.com). Checked .
  14. npm Docs — npm ci (docs.npmjs.com). Checked .
  15. npm Docs — npm rebuild (docs.npmjs.com). Checked .
  16. Docker docs — Build context, .dockerignore files (docs.docker.com). Checked .
  17. bcrypt.js v3.0.3 — README (github.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Node app

Got a question? — @livops_uz