3

I worked through a very simple mongodb tutorial in which we used the mongo shell to create a db, create a collection, and then wrote a very simple nodejs program to access the collection and log the documents to the console.

var mongodb = require('mongodb');

var db = new mongodb.Db('mcfly', new mongodb.Server('127.0.0.1', 27017), {safe: true});

db.open(function(err) {
  db.collection('mathpeeps', function(err, collection){
    collection.find().toArray(function(err, items){
  console.log(items)
    });
  });
});

But when I run the program it gives me:

js-bson: Failed to load c++ bson extension, using pure JS version
[ { _id: 53ab29bbdb49942ae25b5201,
    firstname: 'Leonard',
    lastname: 'Euler',
    born: 1707 },
  { _id: 53ab29fedb49942ae25b5202,
    firstname: 'Karl',
    lastname: 'Gauss' } ]

I've googled and searched SO for "js-bson: Failed to load c++ bson extension, using pure JS version" and tried everything except answers that didn't work for others or answers that were centered around issues with mongoose as I'm not using mongoose in this exercise.

Some of the things I've tried so far:

sudo apt-get install gcc make build-essential
rm -rf node_modules
npm cache clean
npm install

(but this exercise doesn't even use a package.json file)


I tried re-installing node-mongodb with the most recent version


I tried

npm install -g node-gyp

and I tried an answer at SO here Failed to load c++ bson extension :

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

node-gyp rebuild

and that produced:

gyp info it worked if it ends with ok
gyp info using node-gyp@0.13.1
gyp info using node@0.11.9-pre | linux | x64
gyp ERR! configure error 
gyp ERR! stack Error: "pre" versions of node cannot be installed, use the --nodedir flag     instead
gyp ERR! stack     at install (/usr/local/lib/node_modules/node-    gyp/lib/install.js:66:16)
gyp ERR! stack     at Object.self.commands.(anonymous function) [as install]     (/usr/local/lib/node_modules/node-gyp/lib/node-gyp.js:66:37)
gyp ERR! stack     at getNodeDir (/usr/local/lib/node_modules/node-    gyp/lib/configure.js:152:20)
gyp ERR! stack     at /usr/local/lib/node_modules/node-gyp/lib/configure.js:95:9
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:659:7)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:106:17)
gyp ERR! stack     at maybeClose (child_process.js:773:16)
gyp ERR! stack     at Socket.<anonymous> (child_process.js:986:11)
gyp ERR! stack     at Socket.EventEmitter.emit (events.js:103:17)
gyp ERR! stack     at Pipe.close (net.js:458:12)
gyp ERR! System Linux 3.13.0-29-generic
gyp ERR! command "node" "/usr/local/bin/node-gyp" "rebuild"
gyp ERR! cwd /home/nathaniel/node_modules/mongodb/node_modules/bson
gyp ERR! node -v v0.11.9-pre
gyp ERR! node-gyp -v v0.13.1
gyp ERR! not ok 

I saw a SO answer here Failed to load c++ bson extension, using pure JS version saying:

"Sounds like you didn't have gcc/g++/make/python 2.x installed when you used npm to install the bson module. Try installing them first and then use npm to reinstall bson."

But this answer wasn't selected as a solve and I have no idea what this means as I'm a serious noob.


I really want to continue learning mongodb but I don't want some package bug to cause problems down the road and make me mistakenly think I'm doing something wrong. I would really like to get this cleared up now.

I also noticed that many of the other people posting this issue (without mongoose or express involvement) have not selected an answer or replied that they fixed the issue.

I'm running Ubuntu 14.04 LTS and node v0.11.9-pre

Any help will be greatly appreciated!!!!

Community
  • 1
  • 1
CoolestUsername
  • 195
  • 2
  • 11

1 Answers1

2

I think the error is pretty clear: Error: "pre" versions of node cannot be installed, use the --nodedir flag instead. So you should either supply the path to the root node source directory that you installed node from or install/use an actual node release (v0.11.13 is the latest unstable release and v0.10.29 is the latest stable release as of this writing) and try npm install/node-gyp rebuild again.

The reason it asks for the node source directory is because node-gyp has to download a node source tarball from the nodejs.org site to be able to compile addons (it needs the C++ header files, etc). However if you have a node version that is a pre-release, node-gyp has no way to know what source tree to download in order to match what you have installed.

mscdex
  • 104,356
  • 15
  • 192
  • 153