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.
Related messages for the same problem
| 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
installinlivops.json. See Frameworks and livops.json. - An uploaded folder never brings node_modules. When you upload a folder or a
.zip,node_modulesis dropped automatically; see Ways to deploy. - A GitHub checkout skips node_modules too. When Livops fetches a repository, a committed
node_modulesfolder is left out of the checkout, so causes 2 and 3 cannot reach the build. - bcrypt gets a compiler. When
package.jsonlistsbcrypt, Livops builds in an image that has the compiler node-gyp needs. If the build log still saysgyp ERR! find Python, the docs’ answer isbcryptjsinstead ofbcrypt, 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
.nvmrcand.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 withnpm 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
- Node.js docs — Errors, ERR_DLOPEN_FAILED (nodejs.org). Checked .
- Node.js v24.16.0 source — src/node_binding.cc (github.com). Checked .
- glibc 2.41 source — elf/dl-load.c (sourceware.org). Checked .
- glibc 2.41 source — dlfcn/dlerror.c (sourceware.org). Checked .
- musl 1.2.5 source — ldso/dynlink.c (git.musl-libc.org). Checked .
- musl 1.2.5 source — src/errno/__strerror.h (git.musl-libc.org). Checked .
- bcrypt v5.1.1 source — package.json (github.com). Checked .
- node-pre-gyp v1.0.11 source — lib/install.js (github.com). Checked .
- node-pre-gyp v1.0.11 — README (github.com). Checked .
- bcrypt v6.0.0 — CHANGELOG (github.com). Checked .
- bcrypt v6.0.0 source — package.json (github.com). Checked .
- unpkg — [email protected] package files, prebuilds (unpkg.com). Checked .
- node-gyp-build v4.8.4 source — node-gyp-build.js (github.com). Checked .
- npm Docs — npm ci (docs.npmjs.com). Checked .
- npm Docs — npm rebuild (docs.npmjs.com). Checked .
- Docker docs — Build context, .dockerignore files (docs.docker.com). Checked .
- 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 appRelated
- gyp ERR! build error / gyp ERR! find Python: node-gyp rebuild failed during npm install, how to fix itgyp ERR! build error, gyp ERR! find Python or gyp ERR! stack Error: not found: make means npm install tried to compile a native addon such as bcrypt, sqlite3 or sharp, and the machine has no Python, make or C++ compiler. It usually happens because no prebuilt binary fits your Node version or platform (Alpine/musl included). Upgrade to a release with prebuilt binaries, switch to a pure-JavaScript package such as bcryptjs, or install the build tools.
- exec format error in Docker: the image was built for another CPUThe kernel refused to run the container's program because it is not an executable for this machine: almost always a binary or image built for arm64 (an Apple Silicon Mac) running on an amd64 server, or the reverse. Build for the server's platform with --platform linux/amd64, or as a multi-platform image; if the file is a script, give it a #! line.
- Error: Could not load the "sharp" module using the linux-x64 runtime — how to fix itCould not load the "sharp" module using the linux-x64 runtime means sharp found no prebuilt binary for the Linux machine it runs on. Since sharp 0.33 that binary is a separate optional package, @img/sharp-linux-x64, and it goes missing when node_modules was installed on a Mac or Windows and copied over, when the lockfile was made on another platform, or when optional dependencies were skipped. Install on the Linux machine with optional dependencies included, or add the Linux package to the lockfile.
- npm ci can only install packages when your package.json and package-lock.json are in sync: how to fix itnpm ci can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync (npm error code EUSAGE) means the committed lockfile no longer matches package.json: a dependency was added or changed without re-running npm install, or the lockfile was not committed. Run npm install locally, commit package.json and package-lock.json together, and npm ci passes.
- Error: Cannot find module (MODULE_NOT_FOUND) in Node.js: how to fix itError: Cannot find module 'x' with code MODULE_NOT_FOUND, or ERR_MODULE_NOT_FOUND with import, means Node.js looked for a package or file and it was not there. On a server the usual causes are a package missing from package.json, a devDependency the install skipped, a path whose case differs from the file, a start command pointing at a file that was never built, or an ES module import without its .js extension.
- Docker hosting: how to deploy a Docker container or a DockerfileTo host a container you need a host that builds or pulls the image, starts its CMD, sends traffic to the port it listens on and stops it with SIGTERM. On Livops a Dockerfile at the root of the repository is built on Livops's servers and runs for 39 credits a month (39,000 so'm); a public image from Docker Hub, GHCR or Quay runs without a build. Here is what a container needs, and how a VPS, Render, Railway, Fly.io, DigitalOcean and Heroku compare.
- Node.js hosting in 2026: how to deploy an Express appA Node.js server in production needs one long-running process that listens on the port it is given, restarts when it crashes and exits cleanly on SIGTERM. On Livops you push a repository with package.json and it runs as a web app for 39 credits a month (39,000 so'm). Here is what Node needs, and how a VPS, Render, Railway, Fly.io, DigitalOcean and Heroku compare.
- NestJS hosting in 2026: how to deploy a NestJS app, and whereA NestJS app in production is built to dist once and started with node dist/main, listens on process.env.PORT on every interface, answers a health check that respects its global prefix, and migrates its Prisma or TypeORM schema before it takes traffic. On Livops a Nest repository builds and starts from a push, for 39 credits a month (39,000 so'm). Here is how Render, Railway, Fly.io and a VPS with pm2 compare.
- sh: 1: vite: Permission denied (also next, tsc, react-scripts, nest): exit code 126 and the fixsh: 1: vite: Permission denied, with exit code 126, means the shell found node_modules/.bin/vite but was not allowed to run it: the file has no execute permission. Almost always node_modules was not installed on the machine that builds, but committed, zipped or copied from another one, often a Windows PC, and the execute bit was lost on the way. Keep node_modules out of git, uploads and images, and run a clean npm ci where the build runs. chmod +x is only a stopgap.