mongodb-memory-server will not start under Jest or in Docker
libcurl4 is not available on your system. Mongod requires it and cannot be started without it.Short answer
When mongodb-memory-server fails to start, the real reason is hidden behind a generic message. Turn on MONGOMS_DEBUG, then match the output: libcurl4 missing in a Docker image, Alpine Linux, a wrong MONGOMS_SYSTEM_BINARY path, or too many Jest workers starting servers at once.
Fix it fast
1Find out which cause you have
Turn on the library's debug output and run one test file alone, so the real error from mongod is printed instead of a generic failure.
MONGOMS_DEBUG=1 npx jest --runInBand path/to/one.test.js2Match what you see, then jump to its fix
- 1
libcurl4 is not available on your system. Mongod requires it and cannot be started without it.libcurl4 is missing in the Docker or CI imageFix - 2
The download or start fails on node:*-alpine; there is no MongoDB build for Alpine, and the Ubuntu build does not run there.The image is Alpine LinuxFix - 3
NoSystemBinaryFoundError, or an InsufficientPermissionsError for the binary.MONGOMS_SYSTEM_BINARY points at a missing or non-executable fileFix - 4
Start-up errors show up in a full parallel run on a many-core machine, and are rarer when you run the files one by one.Every Jest worker starts its own serverFix
- 1
What does the error look like?
libcurl4 is not available on your system. Mongod requires it and cannot be started without it.NoSystemBinaryFoundError
Cause 1 of 4
libcurl4 is missing in the Docker or CI image
1Check
The docs say that since version 5.0.0 most distributions need libcurl4 installed by hand when the image is not based on a node image. Check inside the image.
ldconfig -p | grep libcurl2Fix
Install libcurl4 in the image.
# Dockerfile (Debian or Ubuntu base)
RUN apt-get update && apt-get install -y libcurl43Verify
Rebuild the image and run the tests again.
docker build -t my-tests . && docker run --rm my-tests npx jestConfirmed by typegoose.github.io
Cause 2 of 4
The image is Alpine Linux
1Check
Look at the OS inside the container.
cat /etc/os-release2Fix
Switch the test image to a non-Alpine one, or point the library at a mongod binary that already runs on the system with MONGOMS_SYSTEM_BINARY.
# Dockerfile: use a non-Alpine base
FROM node:22
# or keep the image and use a mongod you installed yourself
MONGOMS_SYSTEM_BINARY=/usr/bin/mongod npx jest3Verify
Run the tests inside the new image.
MONGOMS_DEBUG=1 npx jest --runInBandConfirmed by typegoose.github.io
Cause 3 of 4
MONGOMS_SYSTEM_BINARY points at a missing or non-executable file
1Check
NoSystemBinaryFoundError means a binary was set but nothing exists at that path or it cannot be executed. InsufficientPermissionsError means the file exists without execute permission.
echo "$MONGOMS_SYSTEM_BINARY"
ls -l "$MONGOMS_SYSTEM_BINARY"2Fix
Correct the path to a real mongod, or unset the variable so the library downloads its own binary. Add execute permission if the file is right but not executable.
unset MONGOMS_SYSTEM_BINARY
# or: chmod +x /path/to/mongod3Verify
Run one test file with debug output.
MONGOMS_DEBUG=1 npx jest --runInBand path/to/one.test.jsConfirmed by typegoose.github.io
Cause 4 of 4
Every Jest worker starts its own server
1Check
The docs warn that the number of spawned Jest workers must be limited on machines with many cores. Check how your test run is configured.
grep -n "maxWorkers\|globalSetup" package.json jest.config.*2Fix
Start one shared instance in Jest globalSetup and stop it in globalTeardown, put its URI in an environment variable, and limit the workers.
// globalSetup.js
const { MongoMemoryServer } = require('mongodb-memory-server');
module.exports = async () => {
const mongod = await MongoMemoryServer.create();
global.__MONGOINSTANCE = mongod;
process.env.MONGO_URI = mongod.getUri();
};
// globalTeardown.js
module.exports = async () => { await global.__MONGOINSTANCE.stop(); };
// run
npx jest --maxWorkers 43Verify
Run the whole suite twice in a row.
npx jest --maxWorkers 4Confirmed by typegoose.github.io
How do I stop it coming back?
- Pin the test image and keep libcurl4 in its Dockerfile, so a base-image change does not break CI.
- Create the memory server once per Jest run, in globalSetup, not once per test file.
- Keep MONGOMS_DEBUG=1 in the CI job for a failing run, and drop it when it is green.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Databases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
Node.js in Docker Compose cannot find mysql2/promise
The app works on the host but fails after docker compose up
mysql2/promise is a path inside the mysql2 package.
2 causes, each with a fixDatabases
The MySQL container stops with: the designated data directory is unusable
[ERROR] --initialize specified but the data directory exists. Aborting.
This message closes a failed first start.
3 causes, each with a fixDatabases
node-oracledb DPI-1047 and libclntsh.so cannot open shared object file
DPI-1047: Cannot locate a 64-bit Oracle Client library
DPI-1047 means Thick mode was switched on but the Oracle Client library could not be loaded.
5 causes, each with a fixDatabases
PHP throws "could not find driver" when it opens a PDO connection
Fatal error: Uncaught PDOException: could not find driver in /var/www/app/db.php:12
PDO is only a layer; each database needs its own driver extension, such as pdo_mysql or pdo_pgsql, and the DSN prefix must name a driver that is loaded.
4 causes, each with a fixDatabases
A Postgres query in Docker fails with: could not resize shared memory segment
ERROR: could not resize shared memory segment: No space left on device
Postgres in a container hands query memory to parallel workers through the container's /dev/shm.
2 causes, each with a fix