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

前端 未结 30 2346
予麋鹿
予麋鹿 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:37

    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.

提交回复
热议问题