Failed to load c++ bson extension

Failed to load c++ bson extension

A total node noob here. I’ve been trying to set up a sample node app but the following error keeps popping up every time I try to run:

node app

Failed to load c++ bson extension, using pure JS version

throw er; // Unhandled ‘error’ event
Error: failed to connect to [#$%67890 :27017]
at null. (/home/thejazeto/code/nodejs/authen/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:553:74)
at EventEmitter.emit (events.js:106:17)
at null. (/home/thejazeto/code/nodejs/authen/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
at EventEmitter.emit (events.js:98:17)
at Socket. (/home/thejazeto/code/nodejs/authen/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:512:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:830:16
at process._tickCallback (node.js:415:13)


Solution 1:

I guess you did not have the make tools available when you installed your mongodb library. I suggest you do

xcode-select --install (on a mac)
or sudo apt-get install gcc make build-essential (on ubuntu)

and run

rm -rf node_modules
npm cache clean
npm install

OR just npm update based on @tobias comment (after installing build-essential)

npm update

Solution 2:

I just resolved that.

When you install the mongoose module by npm, it does not have a built bson module in it’s folder. In the file node_modules/mongoose/node_modules/mongodb/node_modules/bson/ext/index.js, change the line

bson = require('../build/Release/bson');


bson = require('bson');

and then install the bson module using npm.

Related:  Use HTML5 to resize an image before upload

Solution 3:

I have sorted the issue of getting the “Failed to load c++ bson extension” on raspbian(debian for raspberry) by:

npm install -g node-gyp

and then

npm update

Solution 4:

I was unable to solve this

until now. First of all you have to have system packages mentioned by Pradeep Mahdevu. Those are:

xcode-select --install (on a mac) 


sudo apt-get install gcc make build-essential (on ubuntu)

Then I’ve installed node-gyp

npm install -g node-gyp 

like datadracer said but npm update also suggested by him is risky. It update all modules, which can be dangerous (sometimes API changes between versions).

I suggest going into node_modules/mongodb/node_modules/bson directory and from there use

node-gyp rebuild

That solved the problem for me.

Solution 5:

A common problem is that node-gyp requires Python 2.x and if your system’s python points to 3.x, it will fail to compile bson, without warning. You can fix this by setting a python global key in your npm config that points to the 2.x executable on your system. For example, on Arch Linux:

npm config -g set python "/usr/bin/python2"

Solution 6:

On WIN 8.1

Related:  Detect if parameter passed is an array? Javascript [duplicate]

It seems I used a wrong version of mongoose in my package.json file.

I removed the line “mongoose” : “^3.8.15” from package.json

npm install mongoose –save

Now it says “mongoose”: “^4.0.6” in package.json and the error I had is gone.