186

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

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: failed to connect to [#$%67890 :27017]
    at null.<anonymous> (/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.<anonymous> (/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.<anonymous> (/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)
cn007b
  • 16,596
  • 7
  • 59
  • 74
Theja
  • 2,448
  • 3
  • 14
  • 18
  • 3
    The error is `failed to connect to`, so I think that `bson` message may be unrelated and not actually matter. Are you sure your Mongo connection settings are correct? – loganfsmyth Feb 09 '14 at 07:43
  • 1
    Funny story I only get this error on a windows machine... havent tried installing node-gyp but ive tried almost everything else and still getting the error. and chocoletey wont install build essentials – Snymax Feb 20 '15 at 19:08
  • Isn't the JS BSON code almost as fast as the C++ now? If so, is this really a problem? – UpTheCreek Feb 24 '15 at 09:06
  • For people on newer iojs experiencing this issue, I have opened a ticket to track this issue: https://github.com/mongodb/js-bson/issues/136 – bitinn Jul 23 '15 at 07:16
  • Note: I am using Keystone.js as my mvc framework. For me, you change "../build/Release/bson" to "../browser_build/bson". If you go up, you see the browser_build folder. – Pranay Pant Feb 11 '17 at 01:00

35 Answers35

201

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
Neuquino
  • 11,580
  • 20
  • 62
  • 76
Pradeep Mahdevu
  • 7,613
  • 2
  • 31
  • 29
  • 2
    +1. All I had to do was the last 3 lines - I think the reason mine wasn't built was because it came as part of the `mongoskin` module. – Matt Browne Jun 02 '14 at 20:08
  • 2
    Wow! That ``sudo apt-get install gcc make build-essential`` tip is one of the best Node.js + Ubuntu development tips I've ever seen. It's an absolute game-changer if you're used to development web applications and just developing with Node.js on Ubuntu. – Charney Kaye Aug 09 '14 at 19:35
  • 3
    What is the reason of installing `gcc` and `make` along with `build-essential`? The latter depends on the other two so they're going to be installed anyway (http://packages.ubuntu.com/trusty/build-essential). Doing `sudo apt-get install build-essential` should be enough. – Sergey Sep 18 '14 at 07:25
  • try re-installing mongoose: `npm install mongoose --save` – knutole Feb 16 '15 at 16:24
  • Problem is still here on osx yosomite. – jeremieca Feb 23 '15 at 10:25
  • That's a quick solution. `xcode-select --install` on mac and then `npm update` fixed it. Thank you. – Khay Mar 12 '15 at 06:35
  • 8
    How about on windows?? I am getting something like this on my windows command prompt C:\Users\me>node C:\Users\me\Desktop\nodeproject\datagen.js { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson: Failed to load c++ bson extension, using pure JS version { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson: Failed to load c++ bson extension, using pure JS version Connected correctly to server – ULLAS K Apr 08 '15 at 12:50
  • Is there a way to make it a correct answer? This happens on my mac once and my ubuntu now and I solved the problem with this solution both times. – Shih-Min Lee May 19 '15 at 02:08
  • 1
    What about window machines :( – Jamie Hutber Jul 17 '15 at 09:35
  • For people on newer iojs experiencing this issue, I have opened a ticket to track this issue: https://github.com/mongodb/js-bson/issues/136 – bitinn Jul 23 '15 at 07:17
100

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');

to

bson = require('bson');

and then install the bson module using npm.

Efrain
  • 3,248
  • 4
  • 34
  • 61
user1548357
  • 1,089
  • 1
  • 6
  • 2
  • 36
    is it a good idea to change things inside the node_modules directory? That seems brittle – Douglas Ferguson Dec 29 '14 at 22:37
  • 6
    @DouglasFerguson Not really. If you edit a node_module, I'd suggest forking the repo and installing your modified module via npm/bower. – Christopher Marshall Jan 22 '15 at 19:50
  • Yes. I agree. I was just pointing out that the previous advice could encourage others to edit node_modules directly. – Douglas Ferguson Jan 23 '15 at 20:49
  • 4
    This answer worked. Devs at Mongoose, please fix this! – Stephan Kristyn Feb 10 '15 at 13:52
  • 24
    This is a hack, not a fix. – Ashesh Mar 04 '15 at 09:06
  • 10
    This is not a solution. With the same result you can just remove strings which prints error. This error appears because of native c++ plugin for bson not found and in this case js realization will be used anyway. – Maksim Nesterenko May 19 '15 at 00:38
  • I was doing this on a release of [DietPi](http://fuzon.co.uk/phpbb/viewtopic.php?f=8&t=6) on a 512MB RP1. Just wanted to mentioned that I had to install **Python** (`apt-get install python`) and then from within `node_modules/mongoose/node_modules/mongodb/node_modules/bson/` run `make`. This created `Release/bson.node`. – staticboy Jul 09 '15 at 16:42
  • Solved my problem here. Better make a fork to have this fix. – Antonio Brandao Jul 14 '15 at 18:57
  • But then I updated connect-mongo to 0.8.2 and mongoose to version 4.0.7, and it worked without the hack! – Antonio Brandao Jul 14 '15 at 19:03
  • I did this change and it worked only if I replace all the files containing the "require('../build/Release/bson')" to require('bson'). The files that I changed are: node_modules/mean-connect-mongo/node_modules/mongodb/node_modules/bson/ext/index.js, node_modules/bson/ext/index.js – myxlptlk Mar 17 '16 at 07:15
  • @Ashesh so what? we are not pushing it to the repo or will experience the issue next time. we are fixing a temporary bug in the environment in a temporary way, I think it's harmless. – technophyle Sep 13 '16 at 03:58
  • I dont have a bson module under `node_modules/mongoose/node_modules/mongodb/node_modules` folder despite installing mongoose, mongodb, bson... – jsky Sep 25 '16 at 18:37
36

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
Daniele Urania
  • 2,658
  • 1
  • 15
  • 11
27

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) 

or

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.

mbochynski
  • 706
  • 7
  • 15
  • 5
    What about windows :( – Jamie Hutber May 18 '15 at 11:17
  • @JamieHutber Install VS 2013 – Rahil Wazir Jul 08 '15 at 09:27
  • I was doing this on a release of [DietPi](http://fuzon.co.uk/phpbb/viewtopic.php?f=8&t=6) on a 512MB RP1. Just wanted to mentioned that I had to install **Python** (`apt-get install python`) and then from within `node_modules/mongoose/node_modules/mongodb/node_modules/bson/` run `make`. This created `Release/bson.node`. – staticboy Jul 09 '15 at 16:43
20

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"
neverfox
  • 6,680
  • 7
  • 31
  • 40
19

On WIN 8.1

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.

CLI:

npm install mongoose --save

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

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Oak
  • 352
  • 4
  • 13
  • Thank you! You saved me from countless hours of head bashing on the keyboard. This was the last and only solution that worked on windows. The issue was that I was running a version 3.x of mongoose which appeared to be incompatible with the latest mongodb install on my pc. – ChallengeAccepted Jul 07 '15 at 20:51
  • This finally got me running on Ubuntu as well. – Jason Kennaly Dec 20 '15 at 06:30
  • Yes, running on Mac as well . Thanks! – jos Sep 23 '16 at 09:19
  • Thanks a bunch! Following on Microsoft's "You’ve Got Documents! A MongoDB Jump Start" training session and had the same error, only related to the mongodb package - did the above steps for that one and sure enough, it's now running! :) – Fernando Madruga Dec 10 '16 at 22:59
11

I'm running Ubuntu 14.04 and to fix it for me I had to create a symlink for node to point to nodejs as described here:

nodejs vs node on ubuntu 12.04

Once I did that I re-ran these commands:

rm -rf node_modules
npm cache clean
npm install
Community
  • 1
  • 1
Anthony
  • 446
  • 6
  • 15
  • Great Anthony! I'm also running on Ubuntu 14.04 and your suggestion solved the problem for me. It is not enough to install build-essential and reinstall everything: I also had to add the 'node' ln. – Franco Jul 11 '15 at 15:51
8

So in my case, I first tried to check under this directory /node_modules/mongoose/node_modules/, just to confirm that I have the bson module. I figured out that I did not have it in the first place, then I just run

npm install bson 

and then

npm update

All got sorted.Tried and tested in Ubuntu.

Steven
  • 81
  • 1
  • 1
8

just wanted to say I also had the error

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

But with none of the other errors. I tried everything and turns out the mongodb drivers that I was specifying in the package.json file was incompatible with my version of MongoDB. I changed it to my latest version which was (1.4.34) and it worked!!!

the pickle
  • 668
  • 8
  • 18
8

sudo npm rebuild was what fixed it for me.

Zach Dahl
  • 609
  • 5
  • 12
8

I finally corrected this error by updating my mongodb dependency version to ~2.0.36 in package.json.

 "dependencies": {
    "consolidate": "~0.9.1",
    "express": "3.x",
    "mongodb": "~2.0.36",
    "mongoose": "^4.1.12"
  }
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Feezy23
  • 81
  • 1
  • 4
5

Unfortunately, All the above answers are only half right.. Took a long time to figure this out..

Mongoose bson install via npm throws warning and causes the error...

npm install -g node-gyp

git clone https://github.com/mongodb/js-bson.git
cd js-bson
npm install
node-gyp rebuild

This works like magic!!

  • This fails with error: `$ node gyp rebuild module.js:341 throw err; ^ Error: Cannot find module '/private/tmp/js-bson/gyp' at Function.Module._resolveFilename (module.js:339:15) at Function.Module._load (module.js:290:25) at Function.Module.runMain (module.js:447:10) at startup (node.js:140:18) at node.js:1001:3` – Thomas Modeneis May 07 '16 at 15:05
4

For me it only take to run these commands in my api directory:

rm -rf node_modules
npm cache clean 
npm install
4

I just ran:

sudo npm install bson

and

sudo npm update

and all become ok.

cn007b
  • 16,596
  • 7
  • 59
  • 74
  • You should very much avoid running npm with sudo rights. – loganhuskins Mar 28 '16 at 13:39
  • Anything that you can avoid having in the sudo space, you should. I don't think it'll cause any dire issues, it's just a good practice. Here's an article that may help (can't vouch for it other than liking John Papa). http://www.johnpapa.net/how-to-use-npm-global-without-sudo-on-osx/ – loganhuskins Mar 28 '16 at 16:56
3

The bson extension message is just a warning, I get it all the time in my nodejs application.

Things to check:

  • MongoDB instance: Do you have a MongoDB instance running?
  • Config: Did you correctly configure Mongoose to your MongoDB instance? I suspect your config is wrong, because the error message spits out a very weird string for your mongodb server host name..
hendrikswan
  • 2,263
  • 1
  • 20
  • 25
  • That was useful information. With that out of my way I figured out that I was mixing up the mongolab user account info with the actual mongodb user info for the database. – Theja Feb 10 '14 at 10:46
  • Hmm, i'm running into the same issue. Everything was working on my local test server though and I'm using mongoHQ so have no need for a local mongo instance, what else could be going wrong? – Karoh Jun 09 '14 at 22:05
  • forget about that, Pradeep's answer below solved that :) – Karoh Jun 09 '14 at 22:11
3

I fixed it by changing line 10 of:

/node_modules/mongoose/node_modules/mongodb/node_modules/bson/ext/index.js 

from:

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

to:

bson = require('bson');
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
jorgefpastor
  • 307
  • 3
  • 9
3

I fixed this problem on CentOS by

  • sudo yum groupinstall "Development Tools"
  • sudo npm install -g node-gyp
  • rm -r node_modules
  • npm cache clean
  • npm install
liudong
  • 131
  • 1
  • 6
2

I also got this problem and it caused my sessions not to work. But not to break either...

I used a mongoose connection.

I had this:

var mongoose = require('mongoose');
var express = require('express');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
var MongoStore = require('connect-mongo')(expressSession);
...
var app = express();
app.set('port', process.env.PORT || 8080);
app.use(bodyParser);
mongoose.connect('mongodb://localhost/TEST');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  console.log('MongoDB connected');
});


app.use(cookieParser());
app.use(expressSession({
  secret: 'mysecret',
  cookie: {
    maxAge: null,
    expires: moment().utc().add('days',10).toDate(),// 10 dagen
  },
  store: new MongoStore({
  db: 'TEST',
  collection: 'sessions',
}),

Very straightforward. But req.session stayed always empty.

rm -rf node_modules
npm cache clean
npm install

Did the trick. Watch out you dont have a 'mongodb' in your package.json! Just Mongoose and connect-mongo.

KLoozen
  • 364
  • 2
  • 4
2

Here's how I fixed the problem on Ubuntu:

  1. ln -s /usr/bin/nodejs /usr/bin/node
  2. npm install node-gyp
  3. cd node_modules/mongodb/node_modules/bson
  4. node-gyp rebuild

Inspired by @mbochynski answer, but I had to create a symbolic link first, otherwise the rebuild failed.

collimarco
  • 34,231
  • 36
  • 108
  • 142
2

i was having same trouble tried so many options but in the last npm intall in my mean app folder worked.

ashishkumar148
  • 975
  • 1
  • 10
  • 26
2

I had this problem because I was including the node_modules folder in my Git repository. When I rebuilt the node_modules on the other system it worked. One of them was running Linux, the other OS X. Maybe they had different processor architectures as well.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
marcmtlca
  • 21
  • 1
1

I had the same problem on my EC2 instance. I think the initial cause was because I had a Node instance running when I installed Mongo. I stopped the Node service and then ran

sudo npm update 

inside of the top level folder of my node project. This fixed the problem and everything was just like new

Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
1

I was trying to run node on virtual machine (vagrant) shared folder. That was a problem. My host machine is Windows, installed node on Windows and worked like a charm. So if you are using virtual machine, just try to run node server on host machine.

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
1

I just had the same problem and literally nothing worked for me. The error was showing kerberos is causing the problem and it was one of the mongoose dependencies. Since I'm on Ubuntu, I thought there might be permission issues somewhere between the globally installed packages -- in /usr/lib/node_modules via sudo, and those which are on the user space.

I installed mongoose globally -- with sudo of course, and everything began working as expected.

P.S. The kerberos package now also is installed globally next to mongoose, however I can't remember if I did it deliberately -- while I was trying to solve the problem, or if it was there from the beginning.

Mahdi
  • 9,247
  • 9
  • 53
  • 74
1

I'm working on Docker with centOS 7, and encountered the same problem.

after looking around, and make several tries, I fixed this problem by installing mongodb, and mongodb-server

yum install mongodb mongodb-server

I don't think this is the best way to produce the minimal container. but I can limit the scope into the following packages

==============================================================================================================
 Package                          Arch              Version                          Repository          Size

==============================================================================================================
Installing:
 mongodb                          x86_64            2.6.5-2.el7                      epel                57 M
 mongodb-server                   x86_64            2.6.5-2.el7                      epel               8.7 M
Installing for dependencies:
 boost-filesystem                 x86_64            1.53.0-18.el7                    base                66 k
 boost-program-options            x86_64            1.53.0-18.el7                    base               154 k
 boost-system                     x86_64            1.53.0-18.el7                    base                38 k
 boost-thread                     x86_64            1.53.0-18.el7                    base                56 k
 gperftools-libs                  x86_64            2.1-1.el7                        epel               267 k
 libpcap                          x86_64            14:1.5.3-3.el7_0.1               updates            137 k
 libunwind                        x86_64            1.1-3.el7                        epel                61 k
 snappy                           x86_64            1.1.0-3.el7                      base                40 k
bolerovt
  • 623
  • 7
  • 18
1

For Windows 7.1, these directions helped me to fix my build environment:

https://github.com/mongodb/js-bson/issues/58#issuecomment-68217275

http://christiankvalheim.com/post/diagnose_installation_problems/

dbasch
  • 1,698
  • 1
  • 15
  • 31
1

I was able to resolve by uninstalling and reinstalling monk package. Initial install seemingly had a corrupt mongodb/bson dependency.

johnkop
  • 11
  • 1
1

On ubuntu 14.04 I needed to create a link in /usr/bin because /usr/bin/env was looking for /usr/bin/node.

ln -s /usr/bin/nodejs /usr/bin/node

The error messages can be found in the builderror.log in each directory so for the message:

bson@0.2.21 install /usr/local/lib/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson (node-gyp rebuild 2> builderror.log) || (exit 0)

look at this file for more information about the exact problem:

/usr/local/lib/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/builderror.log

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Bob
  • 780
  • 9
  • 10
1

The only thing which helps me on Windows 7 (x64): https://stackoverflow.com/a/29714359/2670121

Reinstall node and python with x32 versions.
I spent a lot of time with this error:

Failed to load c++ bson extension

and finally, when I installed module node-gyp (for building native addons) and even installed windows SDK with visual studio - nodejs didn't recognize assembled module bson.node as a module. After reinstalling the problem is gone.

Again, What does this error mean?

Actually, it's even not error. You still can use mongoose. But in this case, instead of fast native realization of bson module, you got js-realization, which is slower.

I saw many tips like: "edit path deep inside node_modules..." - which is totally useless, because it does not solve the problem, but just turned off the error messages.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Maksim Nesterenko
  • 5,661
  • 11
  • 57
  • 91
1

For my case, I npm install all modules on my local machine (Mac), and I did not include node_modules in .gitignore and uploaded to github. Then I cloned the project to my aws, as you know, it is running Linux, so I got the errors. What I did is just include node_modules in .gitignore, and use npm install in my aws instance, then it works.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Jie
  • 317
  • 2
  • 14
1

In our case, the reason that the c++ version bson was not found was because we were behind a corporate proxy and something in the BSON build process needs to reach out to fetch files. When we looked in node_modules/bson/builderror.log, we saw an error like this:

gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: connect ECONNREFUSED
gyp ERR! stack     at errnoException (net.js:904:11)
gyp ERR! stack     at Object.afterConnect [as oncomplete] (net.js:895:19)

Which suggested that the proxy might be the issue. Setting the http_proxy and https_proxy environment variables solved it.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Matt M.
  • 114
  • 2
  • 8
1

Followint @user1548357 I decided to change the module file itself. So as to avoid the problems pointed out by the valid comments below I included my changes in a postinstall script so that I can set it and forget it and be assured that it will run when my modules are installed.

// package.json
"scripts": {
    // other scripts
    "postinstall": "node ./bson.fix.js"
},

and the script is:

// bson.fix.js
var fs = require('fs');
var file = './node_modules/bson/ext/index.js'
fs.readFile(file, 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  var result = data.replace(/\.\.\/build\/Release\/bson/g, 'bson');
  fs.writeFile(file, result, 'utf8', function (err) {
     if (err) return console.log(err);
     console.log('Fixed bson module so as to use JS version');
  });
});
Mike M
  • 4,879
  • 5
  • 38
  • 58
1

easily kick out the problem by just add this line both try and catch block path: node_modules/mongoose/node_modules/mongodb/node_modules/bson/ext/index.js

bson = require('bson');  instead 

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

That is all!!!

Banana
  • 2,435
  • 7
  • 34
  • 60
  • Hi, thanks for your answer. Please use formatting on code examples to make them easier for people to read. – F_SO_K Feb 09 '18 at 09:00
0

If the bson extension wasn't the reason, I guessed the other reason for "failed to connect" would be the user id's. So I created a new database and added a user for the database, with a password for that user (note: not mongolab account password). I updated those on my code and voila! It worked. Duh right? :D

Theja
  • 2,448
  • 3
  • 14
  • 18
0

I also encountered the same problem and I'm a Mac OSX user. Basically, you need to make sure you have installed the Xcode and also the "Command Line Tools" within the Xcode.

Xcode is free and can be downloaded over here: https://developer.apple.com/xcode/downloads/

After you have installed the Xcode, open it and click "Preference" in the pull down menu, and click the "Downloads" icon. Make sure you have the "Command Line Tools" installed.

Then run the following commands like all the other users mentioned above:

rm -rf node_modules
npm cache clean
npm install