LoginSignup
1
3

More than 5 years have passed since last update.

"Error: Cannot find module '../build/Release/bson'"について調べてみた

Last updated at Posted at 2016-10-26

はじめに

Node.js+MongoDBで開発するとよく見かける

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

このエラー。
一瞬こけたかと思うと普通に動作するのでやり過ごしがちですが、気持ち悪いので調べてみました。

StackOverFlowの記事

まず検索すると以下の記事にコードを書き換えちゃえと記載されていました。

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

node_modules\mongodb\node_modules\bson\ext\index.js
の以下の部分を

bson = require('../build/Release/bson');
から
bson = require('../browser_build/bson');
にしろと

実際にコードを確認

※私が確認したコードは若干上記記事と異なり
node_modules/mongoose/node_modules/bson/ext/index.js
mongooseのversionは3.8.35
になります

以下該当ファイルの20行目まで引用

var bson = null;

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

雑なまとめ方をすると第一希望のbsonを探すも、存在しないので補欠のbsonを使っているが、おそらく好ましくないので
ログレベルをエラーにして
`console.error("js-bson: Failed to load c++ bson extension, using pure JS version");'
該当メッセージの出力をしているように思える

c++ bson と pure JS versionの違いとは?

あれこれ検索しているとmongooseの以下のissueを発見

js-bson: Failed to load c++ bson extension, using pure JS version #2285

コミッターのvkarpov15氏によると

Yeah I've noticed this too. Your code will still work, just you may have worse performance. I'll fix this for next version.

動作はするけどパフォーマンスがよくないらしい。

また

"Fixing" this in general isn't really possible, because this error message can mean one of numerous things. It can mean that you don't have a proper version of python or a proper C++ compiler installed (because of node-gyp), it can mean that you switched versions of node from when you npm installed mongoose, etc.

とあり
node-gyp(Pythonを使ったビルドシステム)を動かすためのpython環境かC++のコンパイルに問題がありC++ bson parserが入らないのも原因の模様。

ただし

Upgrade to mongoose >= 4.0.6 so bson doesn't use C++ extensions anymore. Either way, this message is just a warning.

mongooseのバージョンをあげればそもそもこの問題も起きないようです。

実際

aventurellaという方が利用しているライブラリのpackage.jsonを

"mongoose": "~4.4.16",
"mongodb": "~2.1.18",

上記のように変更したところ
現象は解決したそうです。
私も試したところ同様でした。
(もちろんライブラリ自体が上記versionと互換性があるか確認する必要がありますが)

ちなみに
mongoose/mongodbのbsonを見て見たところ、bsonが0.2と古かったので
辻褄があいますね。

参考

js-bson: Failed to load c++ bson extension, using pure JS versionの解決法
Qiita内で類似の事象についての記事です。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3