Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

mongodb-memory-server will not start under Jest or in Docker

Errorlibcurl4 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.

  • Applies to Node.js 22, mongodb-memory-server 10, Jest 29, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.js
  2. 2Match what you see, then jump to its fix

    1. 1libcurl4 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. 2The 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. 3NoSystemBinaryFoundError, or an InsufficientPermissionsError for the binary.MONGOMS_SYSTEM_BINARY points at a missing or non-executable fileFix
    4. 4Start-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

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 libcurl

2Fix

Install libcurl4 in the image.

# Dockerfile (Debian or Ubuntu base)
RUN apt-get update && apt-get install -y libcurl4

3Verify

Rebuild the image and run the tests again.

docker build -t my-tests . && docker run --rm my-tests npx jest

Confirmed by typegoose.github.io

Did this fix it?

Cause 2 of 4

The image is Alpine Linux

1Check

Look at the OS inside the container.

cat /etc/os-release

2Fix

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 jest

3Verify

Run the tests inside the new image.

MONGOMS_DEBUG=1 npx jest --runInBand

Confirmed by typegoose.github.io

Did this fix it?

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/mongod

3Verify

Run one test file with debug output.

MONGOMS_DEBUG=1 npx jest --runInBand path/to/one.test.js

Confirmed by typegoose.github.io

Did this fix it?

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 4

3Verify

Run the whole suite twice in a row.

npx jest --maxWorkers 4

Confirmed by typegoose.github.io

Did this fix it?

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:

Related fixes

Databases

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.