Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson: Failed to load c++ bson extension, using pure JS version

JavascriptAngularjsWindowsnode.jsMongodb

Javascript Problem Overview


I am getting the below error:

{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } 
  js-bson: Failed to load c++ bson extension, using pure JS version

Here are my version details:

  • OS: Windows 7

  • MongoDB: 2.6.5

  • Node: 0.12.0

I have tried these things before I posted the issue here.

  1. I went to \node-modules\mongoose\node-modules\mongodb\node-modules\bson folder and made below change in the binding-gyp file from 'include_dirs': [ '<!(node -e "require(\'nan\')")' ] to 'include_dirs': ["<!(nodejs -p -e \"require('path').dirname(require.resolve('nan'))\")"]

  2. Ran this command npm install -g node-gyp

  3. I've updated the mongoose version to 3.8.21 inside package.json

Nothing works. Please suggest

Javascript Solutions


Solution 1 - Javascript

Find in npm module mongodb:

> ..\node_modules\mongodb\node_modules\bson\ext\index.js

Change path to js version in catch block:

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

To:

bson = require('../browser_build/bson');

Or copy file in:

> ..\node_modules\bson\build\Release\bson

From:

> ..\node_modules\bson\browser_build\bson

Solution 2 - Javascript

I had this issue today (Feb 19th, 2016) and I solved it just by installing the latest version of Mongoose. Try putting this in your package.json:

"mongoose": "~4.4"

Hope that helps. Solved it for me!

Solution 3 - Javascript

The problem is when you install mongoose via npm it assumes you have python installed on your windows and tries to build required libraries. Since you do not have python it skips building phase with a warning. But when you start your application, required modules are not there so you get this error.

In order to do things right first install python (version 2.7) on your computer from: https://www.python.org/downloads/ or if u have installed chockolatey just type choco install python2.

Then make sure your python variable is set. You can set it on command prompt like:

SET python=D:\Python27\python.exe

(Of course you should change the path according to your location of python) Then install node-gyp:

npm install -g node-gyp

Now you can reinstall mongoose or whatever module causing the problem:

npm install mongoose

You will see some yellow lines instead of red ones this time but the error will be gone.

Solution 4 - Javascript

I resolved this issue by installing mogoose version 3.8.23

npm install mongoose@3.8.23

Solution 5 - Javascript

This worked for me. Search in your workspace for the text:

"../build/Release/bson"

You will probably find it inside the mongose and monk modules.

Then replace each:

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

with:

bson = require('bson');

that's all!

Solution 6 - Javascript

Try npm install [email protected] and also replace bson = require('../build/Release/bson'); to

bson = require('../browser_build/bson'); in node_modules/bson/ext/index.js

Solution 7 - Javascript

This worked for me:

Go to the file (in your project):

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

and change:

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

to:

bson = require('bson');

Reference: https://github.com/mongodb/js-bson/issues/118

Solution 8 - Javascript

Short answer

Install the latest version of mongodb.

A little longer answer

Make sure your package.json is using the latest version of mongodb, then remove node_modules/mongodb and do npm install again. If you didn't use mongodb as a direct dependency, try to find which package is using mongdb. I used:

find . -type f -name package.json | xargs grep mongodb
...
./sails-mongo/package.json:    "mongodb": "1.4.26",
...

So I updated ./sails-mongo/package.json to:

"mongodb": "2.1.7",

Then remove node_modules/mongodb and do npm install again. Seems fine now.

Even longer answer

I don't like the current suggested way of using

require('../browser_build/bson')

Since looking at ../browser_build/bson.js, a 4k+ lines file, which seem also a "non-native" implementation. So although it won't spit out any complains, it is still "using pure JS version", which means slower performance.

Looking at https://github.com/mongodb/js-bson/issues/145 and https://github.com/mongodb/js-bson/issues/165, it seems like the issue was caused by:

>antoniofruci commented on Sep 15, 2015 > >I just found out that c++ code has been moved out 6 months ago and it is now an optional dependency: bson-ext. Indeed, if you install latest version no error occurs.

So I tried to remove the whole node_modules and still got the same error. And looking at package.json of node_modules/mongodb, its version is still 1.4.26, not latest 2.1.7.

Apparently my mongodb comes as a dependency of another package I installed: sails-mongo. Modifying the package.json of sails-mongo and redoing npm install finally solve the issue.

Solution 9 - Javascript

Deploying Keystone JS CMS I had same error and I think most elegant solution is this:

Install npm-check-updates:

root@keystonejs:~# npm install -g npm-check-updates

Within your keystone site directory, where package.json is placed, check dependencies:

debian@keystonejs:~/myproject/manalcjim$ npm-check-updates -u

Then update all packages:

debian@keystonejs:~/myproject/manalcjim$ npm install

And finally if you chose jade for templates, maybe you will need install jade module explicitly:

debian@keystonejs:~/myproject/manalcjim$ npm install jade --save

Solution 10 - Javascript

In my case, the bits that come with mongoose (npm install mongoose) have a working version of the mongodb package in its node_modules folder.

The following steps saved me the work of troubleshooting the issue:

  • npm install mongoose
  • copy node_modules\mongoose\node_modules\mongodb to my root node_modules folder (overwriting any version that came with npm install mongodb)
  • ignore the Failed to load c++ bson extension... error (or change the code to be silent on the issue)

Solution 11 - Javascript

The marked answer is completely wrong. All that does is hide the console log statement and does nothing whatsoever do address the actually issue. You can also close your eyes and it will achieve the same result.

The issue is caused by node-gyp and only that. Its purpose is to compile native extensions for certain modules such as bson.

If it fails to do so then the code will fallback to the JS version and kindly tell you so through the informative message:

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

I assume the question is really about how to compile the native C++ extension rather that just not seeing the message so let's address that.

In order for node-gyp to work your node-gyp must be up to date with your node and C++ compiler (that will differ based on your OS). Just as important your module must also be up to date.

First uninstall and reinstall node-gyp

npm un node-gyp -g;npm i node-gyp -g

Now you will need to fully uninstall and reinstall any node module in your app (that includes modules installed by requirements as well) that have bson. That should take care of the error. You can search for 'Release/bson' and find the culprits.

find node_modules/ -type 'f' -exec grep -H 'Release/bson' {} \;

And then uninstall and reinstall these modules.

Easier is to just redo the entire node_modules folder:

 rm -rf node_modules
 npm cache clear
 npm i

If you still experience issues then either 1) your module is out of date - check issue tracker in their repo or 2) you have a potential conflict - sometimes we may have a local node-gyp for example. You can run node-gyp by itself and verify versions.

Solution 12 - Javascript

While creating setup for www.spotdekho.com in new windows10 machine, I was unable to run command "nodemon web.js" due same error

> "Error: Cannot find module '../build/Release/bson'"

Got it fixed by below steps :

> 1. Create folder "Release" inside "node_modules\bson\build" location > 2. Copy bson.js from "node_modules\bson\browser_build" > 4. Paste bson.js inside "node_modules\bson\build\Release" folder.

This will resolve the issue.

PS : I am using mongoose version 4.8.8 .

Thanks :) , hope it helps someone .

Solution 13 - Javascript

When I was getting errors like this, I was upgrading from node v0.10 to node v4.x.x . What I had to do was install a newer version of gcc (I think I had gcc v4.4.x or something). I updated to gcc 4.7.2 and things worked after that.

Solution 14 - Javascript

I also had the same problem with bson.

trying a newer mongoose version

npm install mongoose@4.4

solved the issue.

Massimo.

Solution 15 - Javascript

Run this command to uninstall mongoose npm uninstall --save mongoose - thereafter reinstall it but this time npm automatically installs the latest version of mongoose run npm install --save mongoose This worked for me.

Solution 16 - Javascript

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!!

Solution 17 - Javascript

If you are using windows 8.1, you might want to make sure that you are installing your npm modules with the correct visual studio compiler.

I have Visual Studio 2012 installed, this command works for me. (after deleting node_modules dir)

npm install --msvs_version=2012

For some reason, node-gyp is trying to use the incorrect version of Visual Studio compiler. I also noticed that the "npm install" command was printing out a warnings about not node-gyp dependencies when installing the mongodb and mongoose modules.

After using the correct msvs_version, the npm install warnings went away as well as the console warning when running my nodejs app.

You might also want to make sure that you have the correct Python 2.7.X version installed and not Python 3.0.X.

You will also need to make sure that python is in your env path.

Solution 18 - Javascript

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 many time with this error (Failed to load c++ bson extension) and finally, when I installed module node-gyp (for build native addons) and even installed windows SDK with visual studio - nodejs didn't recognize assembled module bson.node as module. After reinstall the problem is gone.

Again, What means this error?

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 slower. I saw many tips like: "edit path deep inside node_modules..." - which totaly useless because it's not solve the problem but just turned off the error messages.

Solution 19 - Javascript

Try this npm install bson and npm update

Solution 20 - Javascript

You should never change files in NODE_MODULES library folder.

Those files are result of npm install command.

Basicaly, I think for Windows users - right way is use VM. It's what I'm doing when I work from home.

All you need:

for CentOS: yum install gcc gcc-c++ make openssl-devel

for Debian/Ubuntu: apt-get install build-essential

and then npm install or, if you have already done it - npm update

Solution 21 - Javascript

The best solution for me was to upgrade from node-mongodb 1.x to 2.x.

Solution 22 - Javascript

I realize this thread is 1 year + old, but it helped me, albeit with a different module.

I installed mongoose-post-findv0.0.2 and this issue started. To fix it, I used the suggestions and navigated to \node_modules\mongoose-post-find\node_modules\bson\ext and opened the index.js file.

The file starts off with a try catch block trying to require the correct bson version

try {
	// Load the precompiled win32 binary
	if(process.platform == "win32" && process.arch == "x64") {
	  bson = require('./win32/x64/bson');  
	} else if(process.platform == "win32" && process.arch == "ia32") {
	  bson = require('./win32/ia32/bson');  
	} else {
	  bson = require('../build/Release/bson');  
	}	
} catch(err) {
	// Attempt to load the release bson version
	try {
		bson = require('../browser_build/bson');
	} catch (err) {
		console.dir(err)
		console.error("js-bson: Failed to load c++ bson extension, using pure JS version");
		bson = require('../lib/bson/bson');
	}
}

this is the corrected version. What was happening is it tried to load bson from ../build/Release/bson, couldn't find it and fell into the catch. There is tried to load bson again from ../build/Release/bson and of course failed. So I changes the path in the catch to look in ../browser_build/bson. This resolved the error.

I post this for completeness.

Solution 23 - Javascript

I had the same issue after upgrade version of ubuntu to 16.04. I solved in this way, hope it helps.

$rm -rf node_modules
$npm --save install bson
$npm --save install mongoose
$npm install

Solution 24 - Javascript

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.

Sorry for reawakening comments on this, but I wanted to post this for anyone else that hits this in the future since this thread was very helpful in solving our issue.

Solution 25 - Javascript

I'm working with OS Windows 8.1 and I had the same problem. The source of the problem was the version of Python. I found the origin problem review the file ...\node_modules\mongodb\node_modules\mongodb-core\node_modules\kerberos\builderror.log.

I installed the correct version of Python (2.7.9 for 64 bits) and it resolved my problem.

Note: The installed version of python must be equal or greater than 2.7.5 and less than 3.0.0

Solution 26 - Javascript

Tried npm install mongoose --msvs_version=2012, if you have multiple Visual installed, it worked for me

Solution 27 - Javascript

First copy bson.js code from browser_build folder

second create new file bson.js and paste code

third save the new file near to in index.js.

Solution 28 - Javascript

I've tried bson = require('../browser_build/bson'); but end up running into another error

> Cannot set property 'BSON_BINARY_SUBTYPE_DEFAULT' of undefined

Finally I fixed this issue simply by npm update, this will fix the bson module in mongoose.

Solution 29 - Javascript

So, i have the same problem and it happens then the specified mongoDB URL doesn't exist. So, to fix this you need to go "..\server\config\environment" folder and edit in "development.js" file the link to mongoDB.

Example:

// Development specific configuration
// ==================================
module.exports = {
  // MongoDB connection options
  mongo: {
    uri: 'mongodb://localhost/test2-dev'
  },

  seedDB: true
};

So, change this "uri: 'mongodb://localhost/test2-dev'" to some real path to your mongoDB database.

I will be glad if my post will help somebody...

Solution 30 - Javascript

find in npm module mongodb ..node_modules\mongodb\node_modules\bson\ext\index.js

and change path to js version in catch block

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

to bson = require('../browser_build/bson');

try {
    	// Load the precompiled win32 binary
    	if(process.platform == "win32" && process.arch == "x64") {
    	  bson = require('./win32/x64/bson');  
    	} else if(process.platform == "win32" && process.arch == "ia32") {
    	  bson = require('./win32/ia32/bson');  
    	} else {
    	 bson = require('../browser_build/bson');  
    	}	
    } catch(err) {
    	// Attempt to load the release bson version
    	try {
    		bson = require('../browser_build/bson');
    	} catch (err) {
    		console.dir(err)
    		console.error("js-bson: Failed to load c++ bson extension, using pure JS version");
    		bson = require('../lib/bson/bson');
    	}
    }

Solution 31 - Javascript

For today (year 2017) we have that awesome npm module: https://github.com/felixrieseberg/windows-build-tools which usually solves lots of troubles with building of native things for windows.

Try to fix the issue with:

Remove node_modules npm install --global windows-build-tools npm install

Solution 32 - Javascript

I tried this in mongo-sync version 4.1.2

but they made some changes and it is not working with the solutions provided here

the solution which worked for me

Path node_modules/mongo-sync/node_modules/bson/ext/index.js

Old Code

try {
    // Load the precompiled win32 binary
    if (process.platform == "win32" && process.arch == "x64") {
        bson = require('./win32/x64/bson');
    } else if (process.platform == "win32" && process.arch == "ia32") {
        bson = require('./win32/ia32/bson');
    } else {
        bson = require('../build/Release/bson');
    }
} catch (err) {
    // Attempt to load the release bson version
    try {
        bson = require('../build/Release/bson');
    } catch (err) {
        console.dir(err)
        console.error("js-bson: Failed to load c++ bson extension, using pure JS version");
        bson = require('../lib/bson/bson');
    }
}

Change in the try block (copy the bson initialization from catch block)

bson = require('../lib/bson/bson');

So it will look like

try {
	// Load the precompiled win32 binary
	if(process.platform == "win32" && process.arch == "x64") {
	  bson = require('./win32/x64/bson');  
	} else if(process.platform == "win32" && process.arch == "ia32") {
	  bson = require('./win32/ia32/bson');  
	} else {
	  bson = require('../build/Release/bson');  
	}	
} catch(err) {
	// Attempt to load the release bson version
	try {
		bson = require('../lib/bson/bson');
	} catch (err) {
		console.dir(err)
		console.error("js-bson: Failed to load c++ bson extension, using pure JS version");
		bson = require('../lib/bson/bson');
	}
}

Solution 33 - Javascript

First I just find in npm module mongodb the file index.js ..node_modules\mongodb\node_modules\bson\ext\index.js

and change path to js version in catch block

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

but then I just changed it like this, all the similar... previously installed the module... : bson = require('bson');

Solution 34 - Javascript

I also have this problem.

But I change my mongodb version to "^2.2.33" and no longer have the problem.

You shoud npm rm your old mongodb package on global or local and install this version.

If anyone don't want to have any side-effect on changing the direction name could try this method.

Solution 35 - Javascript

For everyone still searching for a solution, I solved it by

bson = require('../../../browser_build/bson.js');

instead of

bson = require(../browser_build/bson.js');

so look if the path is correct.

Solution 36 - Javascript

I solved this error by doing this and it perfectly worked for me. I have created a node App which has the folder node-android/node-modules.So here inside the node-modules directory I have one more directory bson/ext/index.js open this file and you can find inside the catch block.

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

change the above line to

bson = require('../browser_build/bson');

and the error was gone for me and the app is running perfectly without any errors or warnings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnavraView Question on Stackoverflow
Solution 1 - JavascriptanydasaView Answer on Stackoverflow
Solution 2 - JavascriptMatt WestView Answer on Stackoverflow
Solution 3 - JavascriptsalihcenapView Answer on Stackoverflow
Solution 4 - JavascriptRishabh GargView Answer on Stackoverflow
Solution 5 - JavascriptkahlalView Answer on Stackoverflow
Solution 6 - JavascriptRaghav ManikandanView Answer on Stackoverflow
Solution 7 - JavascriptFrancoView Answer on Stackoverflow
Solution 8 - JavascriptPeterView Answer on Stackoverflow
Solution 9 - JavascriptManuel AlcocerView Answer on Stackoverflow
Solution 10 - JavascriptMichaelView Answer on Stackoverflow
Solution 11 - JavascriptcyberwombatView Answer on Stackoverflow
Solution 12 - JavascriptSpotdekhoView Answer on Stackoverflow
Solution 13 - JavascriptB TView Answer on Stackoverflow
Solution 14 - JavascriptMax TamtamView Answer on Stackoverflow
Solution 15 - JavascriptotoloyeView Answer on Stackoverflow
Solution 16 - JavascriptRama PrashanthView Answer on Stackoverflow
Solution 17 - JavascriptJoseph SnowView Answer on Stackoverflow
Solution 18 - JavascriptMaksim NesterenkoView Answer on Stackoverflow
Solution 19 - JavascriptRaghav ManikandanView Answer on Stackoverflow
Solution 20 - JavascriptVictor PerovView Answer on Stackoverflow
Solution 21 - JavascriptjczaplewView Answer on Stackoverflow
Solution 22 - JavascriptPaul StonerView Answer on Stackoverflow
Solution 23 - JavascriptEnrico GiurinView Answer on Stackoverflow
Solution 24 - JavascriptMatt M.View Answer on Stackoverflow
Solution 25 - JavascripthbuenocView Answer on Stackoverflow
Solution 26 - JavascriptAKID AhmedView Answer on Stackoverflow
Solution 27 - JavascriptrktelView Answer on Stackoverflow
Solution 28 - JavascriptunclejimboView Answer on Stackoverflow
Solution 29 - JavascriptVasilic MaximView Answer on Stackoverflow
Solution 30 - Javascriptuser3168528View Answer on Stackoverflow
Solution 31 - JavascriptMaksim NesterenkoView Answer on Stackoverflow
Solution 32 - JavascriptisambitdView Answer on Stackoverflow
Solution 33 - JavascriptFelipeView Answer on Stackoverflow
Solution 34 - JavascriptiamshaojinView Answer on Stackoverflow
Solution 35 - JavascriptVince VerhoevenView Answer on Stackoverflow
Solution 36 - JavascriptPRASANTH MAMAKARView Answer on Stackoverflow