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

前端 未结 30 2224
予麋鹿
予麋鹿 2020-11-28 02:19

I am getting the below error:

{ [Error: Cannot find module \'../build/Release/bson\'] code: \'MODULE_NOT_FOUND\' } 
  js-bson: Failed to load c++ bson extens         


        
相关标签:
30条回答
  • 2020-11-28 02:53

    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.

    0 讨论(0)
  • 2020-11-28 02:53

    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.

    0 讨论(0)
  • 2020-11-28 02:55

    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.

    0 讨论(0)
  • 2020-11-28 02:58

    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)
    0 讨论(0)
  • 2020-11-28 02:59

    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 讨论(0)
  • 2020-11-28 03:00

    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.

    0 讨论(0)
提交回复
热议问题