Mongodb-memory-server: Error: spawn /home/app/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.0.3/mongod ENOENT

Created on 15 Apr 2019  路  11Comments  路  Source: nodkz/mongodb-memory-server

Hello, I have the following code in a mocha test setup file:

before(async function() {
  this.timeout(0);
  const mongod = new MongoMemoryServer({ debug: true });
  const uri = await mongod.getConnectionString();
})

Works well locally, but in circleCI it fails with:

Autostarting MongoDB instance...
Called MongoMemoryServer.start() method:
Called MongoMemoryServer.ensureInstance() method:
Starting MongoDB instance with following options: {"port":42817,"dbName":"5a1789ec-a9a4-4a74-8dc2-c69739468657","uri":"mongodb://127.0.0.1:42817/5a1789ec-a9a4-4a74-8dc2-c69739468657","storageEngine":"ephemeralForTest","dbPath":"/tmp/mongo-mem-68JtuJPtpruwrZ","tmpDir":{"name":"/tmp/mongo-mem-68JtuJPtpruwrZ"}}
MongoBinary options: {"downloadDir":"/home/app/node_modules/.cache/mongodb-memory-server/mongodb-binaries","platform":"linux","arch":"x64","version":"4.0.3"}
MongoBinary: Mongod binary path: /home/app/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.0.3/mongod
Mongo[42817]: Called MongoInstance._launchKiller(parent: 68, child: undefined):
Mongo[42817]: MongodbInstance: is failed: Error: spawn /home/app/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.0.3/mongod ENOENT
  1) "before all" hook in "{root}"
Unhandled rejection TypeError: Cannot read property 'toString' of undefined
    at MongoInstance._launchKiller (node_modules/mongodb-memory-server/lib/util/MongoInstance.js:216:22)

I looked the code at that line and it's the following:

MongoInstance.prototype._launchKiller = function (parentPid, childPid) {
    var _this = this;
    this.debug("Called MongoInstance._launchKiller(parent: " + parentPid + ", child: " + childPid + "):");
    // spawn process which kills itself and mongo process if current process is dead
    var killer = child_process_1.spawn(process.argv[0], [
        path_1.default.resolve(__dirname, '../../scripts/mongo_killer.js'),
        parentPid.toString(),
        childPid.toString(),; // <-- HERE
    ])
}

Any ideas? Thanks!

stale

Most helpful comment

@Seao in my case was the fact that we were using alpine, which the getos package reports as raspbian, and so it download a legacy binary which doesn't seem to work (and throws the ENOENT error).

What I did in my case, was to download and install mongodb in my docker image and set the MONGOMS_SYSTEM_BINARY environment variable. Here's the code in case you want to give it a try:

RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/main' >> /etc/apk/repositories
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/community' >> /etc/apk/repositories
RUN apk update
# mongodb installation throws an error, but seems to work, so ignoring the exit status.
RUN apk add mongodb=4.0.5-r0 || true
# this is required by mongodb-memory-server to avoid trying to download the mongod file.
ENV MONGOMS_SYSTEM_BINARY=/usr/bin/mongod

Hope it helps! If it is for me, we can close this issue 馃憤

All 11 comments

I was able to reproduce this locally by building the docker image, and I can see that the mongod file is there:

docker run api-test ls /home/app/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.0.3/
mongod

I have the same issue with Gitlab runners using [email protected]

Called MongoInstance._launchKiller(parent: 117, child: undefined):
Mongo[46071]: MongodbInstance: is failed: Error: spawn /builds/lab/apostrophe-modules/project-templates/module/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.0.3/mongod ENOENT
...

No such file or directory error while Mongodb binaries are well downloaded and saved at this exact path during npm install.. 馃

@Seao in my case was the fact that we were using alpine, which the getos package reports as raspbian, and so it download a legacy binary which doesn't seem to work (and throws the ENOENT error).

What I did in my case, was to download and install mongodb in my docker image and set the MONGOMS_SYSTEM_BINARY environment variable. Here's the code in case you want to give it a try:

RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/main' >> /etc/apk/repositories
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/community' >> /etc/apk/repositories
RUN apk update
# mongodb installation throws an error, but seems to work, so ignoring the exit status.
RUN apk add mongodb=4.0.5-r0 || true
# this is required by mongodb-memory-server to avoid trying to download the mongod file.
ENV MONGOMS_SYSTEM_BINARY=/usr/bin/mongod

Hope it helps! If it is for me, we can close this issue 馃憤

@machadogj Thanks for the advice, indeed it was related to alpine:3.8 I was using. The thing is, it was implicitly inherited but not relevant at all, so I simply switched to a node:8 image, that is working like a charm 馃憤

Thanks @machadogj ! You saved my day :)

what is the best package to enable cache on circleci?

what is the best strategy to cache this?

@sibelius better to use mongodb-memory-server-global package and add to ci cache the following folder %HOME%/.cache

it would be helpful to have a section in docs like this one https://docusaurus.io/docs/en/publishing#automating-deployments-using-continuous-integration

where you provide a sample setup for circleci, travis and so on

in ~6.1 changed GETOS to an internal re-write, which needs either lsb_release (command) or /etc/os-release file or /etc/*-release (any -release (that is not lsb) file) - so it should work for alpine now (because alpine uses /etc/os-release) BUT mongodb DOES NOT support alpine!

i will close this because (thanks to the getos re-write) it should now detect the right system, but because mongodb dosnt provide a binary for alpine it is still recommended to use a node image

I had this issue at Gitlab, was fixed without other configuration changes simply by just changing the image to the non-alpine one.

From:

test:
  image: node:15-alpine

To:

 test:
  image: node:15

I had this issue at Gitlab, was fixed without other configuration changes simply by just changing the image to the non-alpine one.

From:

test:
  image: node:15-alpine

To:

 test:
  image: node:15

Thank you!

Was this page helpful?
0 / 5 - 0 ratings