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

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

This question is related to javascript angularjs windows node.js mongodb

The answer is


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


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.


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.


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.


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.


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


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.


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!


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


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


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


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


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.


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


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.


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

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\"
  3. 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 .


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.


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.


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

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


I also had the same problem with bson.

trying a newer mongoose version

npm install [email protected]

solved the issue.

Massimo.


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

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

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.


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


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.


Try this npm install bson and npm update


I resolved this issue by installing mogoose version 3.8.23

npm install [email protected]

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!


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.


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.


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)

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


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.


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


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to angularjs

AngularJs directive not updating another directive's scope ERROR in Cannot find module 'node-sass' CORS: credentials mode is 'include' CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response WebSocket connection failed: Error during WebSocket handshake: Unexpected response code: 400 Print Html template in Angular 2 (ng-print in Angular 2) $http.get(...).success is not a function Angular 1.6.0: "Possibly unhandled rejection" error Find object by its property in array of objects with AngularJS way Error: Cannot invoke an expression whose type lacks a call signature

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to mongodb

Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Failed to start mongod.service: Unit mongod.service not found db.collection is not a function when using MongoClient v3.0 MongoError: connect ECONNREFUSED 127.0.0.1:27017 MongoDB: How To Delete All Records Of A Collection in MongoDB Shell? How to resolve Nodejs: Error: ENOENT: no such file or directory How to create a DB for MongoDB container on start up?