158

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

hong4rc
  • 3,999
  • 4
  • 21
  • 40
navra
  • 1,581
  • 2
  • 10
  • 3
  • 2
    I have tried below options suggested by you but no avail. I am getting 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 c:\raj\project\meandemo\node_modules\express\lib\router\route.js:170 throw new Error(msg); ^ Error: Route.get() requires callback functions but got a [object Undefined] at Route. – navra Feb 22 '15 at 08:59
  • 1
    http://stackoverflow.com/questions/21656420/failed-to-load-c-bson-extension/29368230#29368230 – ashishkumar148 Mar 31 '15 at 12:07
  • 1
    Please see this solution, hope it works! As C++ BSON extension is required for several other Modules too. Kindly note that, this solution is specific For Microsoft-OS Win-7 http://stackoverflow.com/a/29714359/4154706 – aitchkhan Apr 18 '15 at 07:52
  • Check the answers in this question too: [Failed to load c++ bson extension](http://stackoverflow.com/q/21656420/1494454) – totymedli Jun 17 '16 at 10:25
  • Possible duplicate of [Failed to load c++ bson extension](http://stackoverflow.com/questions/21656420/failed-to-load-c-bson-extension) – mido Jan 26 '17 at 07:39

36 Answers36

166

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

anydasa
  • 2,616
  • 1
  • 15
  • 13
  • I can not say for sure. Is likely to be working well. This is a bug file paths. – anydasa Mar 17 '15 at 06:33
  • 1
    Awesome! Local environment though, so nothing tragic should happen anyway. – Manatax Mar 17 '15 at 08:15
  • 4
    bson = require('../../../browser_build/bson.js'); – robor Mar 27 '15 at 18:39
  • Thank you. I needed to make a tweak in my case though, i didn't have a browser_build folder in bson_ext folder : My problem was in "node_modules\mongodb\node_modules\mongodb-core\node_modules\bson\node_modules\bson-ext\ext\index.js". I changed the lines to "bson = require('../browser_build/bson');" AND copied the browser_build directory from "node_modules\mongodb\node_modules\mongodb-core\node_modules\bson" (2 folders up) to "node_modules\mongodb\node_modules\mongodb-core\node_modules\bson\node_modules\bson-ext" – Veysel Ozdemir Apr 17 '15 at 15:44
  • This was helpful! I had this problem with a bson installation with mongojs – Bob Aleena May 09 '15 at 01:50
  • 27
    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:59
  • Yes, it is not native use. but this solution – anydasa May 19 '15 at 09:17
  • it's a 32bit vs 64bit thing, the solution is to install 32bit NodeJs and Python on windows 7 64bit. see http://stackoverflow.com/questions/29238424/error-in-npm-install-inspite-of-changing-the-file-bson/29714359#29714359 – Xianlin May 31 '15 at 06:33
  • It's not 32/64 issue. I just installed with binary drivers using all 64 bit. See my comment below with @salihcenap reply. – RichieRich Sep 27 '15 at 05:45
  • still its not working for me..i m using latest version in node and mogodb – albert Jegani Jan 11 '16 at 02:45
  • The error got changed to : TypeError: Cannot set property 'BSON_BINARY_SUBTYPE_DEFAULT' of undefined – Gaurav Lad Feb 08 '16 at 06:25
  • 4
    @Alendorff True, but that is what most of us look for. I don't care about the C++ plugin, I want to get rid of the warnings because they break Node Inspector. From this perspective, your suggestion (removing error prints) is also a solution, because this shouldn't be an error, the code works fine with the JS fallback. – totymedli May 26 '16 at 12:17
  • 3
    Sorry, downvoting as the second best answer here actually has the right, non-hack solution: Install a non-broken version of the package! – jrista Apr 28 '17 at 03:58
  • Replacing bson = require('../build/Release/bson'); to bson = require('../browser_build/bson'); worked for me. – MechaCode Mar 29 '18 at 06:24
71

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!

Matt West
  • 1,376
  • 1
  • 12
  • 18
  • 1
    Worked for me. I think its all a problem with an old mongoose version, which I had from following an old tutorial. IMO this should be the correct answer. – Edward Newell Jun 09 '16 at 23:01
  • 8
    Best answer, all the other ones about editing code within modules are hacks not solutions – bicycle Jun 20 '16 at 21:52
  • 2
    Most straight forward solution without the need to mess with node_modules/mongodb etc. Thanks! – mikeym Jul 01 '17 at 22:55
  • 1
    One of the best, just uninstall mongoose(npm uninstall mongoose) and reinstall it again(npm install mongoose) – Grijan Apr 17 '18 at 07:37
18

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.

JuHwon
  • 2,033
  • 2
  • 34
  • 54
salihcenap
  • 1,927
  • 22
  • 25
  • 2
    The python's version is important. node-gyp doesn't work with version > 3.0 – MatthieuLemoine Jun 19 '15 at 12:37
  • 1
    This is not a full solution... I'm still getting the following error: MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". – orszaczky Jun 21 '15 at 19:17
  • @TheRebel It's assuming vs2010 toolchain. Add `--msvs-version=2012` or `2013e` depending on what you have installed. e.g. `npm install mongoose --msvs-version=2012` – RichieRich Sep 27 '15 at 05:41
  • what to do if i am deploying node app on heroku cloud. – jsbisht Mar 01 '16 at 11:01
15

I resolved this issue by installing mogoose version 3.8.23

npm install mongoose@3.8.23
Ilia Choly
  • 18,070
  • 14
  • 92
  • 160
Rishabh Garg
  • 706
  • 1
  • 9
  • 28
15

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!

fractalwrench
  • 4,028
  • 7
  • 33
  • 49
kahlal
  • 167
  • 1
  • 2
  • I was getting this error trying to run the default tests after setting up a project using meanjs. This fixed it. Thanks. – RaneWrites Apr 02 '17 at 01:30
7

Try npm install mongoose@3.8.23 and also replace bson = require('../build/Release/bson'); to

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

  • Make sure the path to the bson.js is correct. For me bson = require('../browser_build/bson.js'); worked. Also check the version mongoose before going for the installation. – Vikram Shetty Sep 13 '15 at 09:17
7

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

Parker
  • 7,244
  • 12
  • 70
  • 92
Franco
  • 145
  • 1
  • 9
7

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.

Community
  • 1
  • 1
Peter
  • 775
  • 1
  • 6
  • 12
6

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
Manuel Alcocer
  • 131
  • 2
  • 5
5

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)
Michael
  • 3,099
  • 4
  • 31
  • 40
5

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.

cyberwombat
  • 38,105
  • 35
  • 175
  • 251
5

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 .

Geeky Ninja
  • 6,002
  • 8
  • 41
  • 54
Spotdekho
  • 51
  • 1
  • 1
3

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.

B T
  • 57,525
  • 34
  • 189
  • 207
3

I also had the same problem with bson.

trying a newer mongoose version

npm install mongoose@4.4

solved the issue.

Massimo.

Max Tamtam
  • 41
  • 1
3

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.

otoloye
  • 677
  • 7
  • 11
2

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

2

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.

Joseph Snow
  • 2,436
  • 1
  • 21
  • 22
  • This worked for me on Windows 8.1 with Visual Studio Community Edition 2013 using `npm install --msvs_version=2012` – phocks May 26 '15 at 03:02
2

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.

Community
  • 1
  • 1
Maksim Nesterenko
  • 5,661
  • 11
  • 57
  • 91
1

Try this npm install bson and npm update

1

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

Victor Perov
  • 1,697
  • 18
  • 37
1

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

jczaplew
  • 1,715
  • 1
  • 17
  • 21
1

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.

Paul Stoner
  • 1,359
  • 21
  • 44
1

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
Enrico Giurin
  • 2,183
  • 32
  • 30
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.

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.

Matt M.
  • 114
  • 2
  • 8
0

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

mason
  • 31,774
  • 10
  • 77
  • 121
hbuenoc
  • 1
  • 1
0

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

AKID Ahmed
  • 19
  • 1
  • 4
0

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.

Ethaan
  • 11,291
  • 5
  • 35
  • 45
rktel
  • 44
  • 1
0

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.

sqluser
  • 5,502
  • 7
  • 36
  • 50
unclejimbo
  • 91
  • 7
0

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

0

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

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

Maksim Nesterenko
  • 5,661
  • 11
  • 57
  • 91
0

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');
    }
}
isambitd
  • 829
  • 8
  • 14
0

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

Felipe
  • 101
  • 10
  • Changing package codes under node_modules seems like a bad practice – Carl Sep 11 '18 at 03:12
  • yeah ... but it was necessary, in my case... perhaps that was a bug... nothing is perfect .... and that makes it much more fun – Felipe Sep 11 '18 at 08:04
0

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.

moussesj94
  • 485
  • 1
  • 8
  • 27
-1

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.

tbraun89
  • 2,246
  • 3
  • 25
  • 44
Vince Verhoeven
  • 1,693
  • 14
  • 25
-2

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.