Mongoose: Error: Cannot find module 'mongodb/node_modules/bson'

Created on 26 Feb 2015  ·  38Comments  ·  Source: Automattic/mongoose

I have this error since starting to develop with Mongoose 1,5 years ago.

The error never vanished completely, it comes back after each and every npm update

This error has been a friend and annoyance alongside the road developing with MEANB.

Are there any plans to finally get rid of this error or will it continue to annoy the developer community until the end of all time :)?

Most helpful comment

Had to npm install inside node_modules/mongodb.

Thanks for the help!

All 38 comments

Works:

npm install node-gyp -g
npm cache clean
rm -rf node_modules
npm install

Maybe this should go into the official installation docs?

_tl;dr_

npm install node-gyp -g && npm cache clean && rm -rf node_modules && npm install

Can anyone elaborate on this? I have this issue on both Mac and Linux instances. Thanks.

P.S.
I had to set specific version of mongodb in my package json:

"mongodb": "1.4.9"

This also bit me today in deploying a mongoose app to Heroku. It seems Heroku was installing the 4.X mongoose while I still had 3.X locally. Heroku was getting this error on startup and I wasn't. Once I did 'npm update' to mongoose 4.X it started happening locally as well. I backed out to 3.X locally and on Heroku and now things are working again.

I ran into the exact same issue as RationalGeek on a 100% fresh Heroku app. +1 for a better fix than rolling back mongoose

What's the exact error message, with stack trace preferably?

Thanks for looking into this, Valeri.

Steps to reproduce

1.) "rm -rf node_modules" inside main dir of our own project
2.) "npm cache clean"
3.) "npm install" against this packages.json: https://gist.github.com/nottinhill/5a25afb3e3700b79ba1e
4.) Error is thrown. (see below Gist)

Stack Trace and Error message

https://gist.github.com/nottinhill/9851d558092f9d0149e3

Workaround

1.) "npm install -g node-gyp"
2.) cd into "mongoose/**/mongodb-core" dir and pull in bson like this: https://gist.github.com/nottinhill/6ac075b606dea054c053

On Linux:

sudo apt-get update
sudo apt-get install build-essential

On OSX:

xcode-select --install

..then:

npm install -g node-gyp 
cd /to/you/project
rm -rf node_modules
npm install

Works like a charm.

Had to npm install inside node_modules/mongodb.

Thanks for the help!

@nottinhill Thanks for your help.

Its working fine now. not giving me Error .

Thanks
Gaurav

@vkarpov15 It's giving me error again. Please Reopen this issue.

Thank you
Gaurav

What version are you using @gbansal7?

It hit me today.

@Kuzirashi which version of mongoose?

3.8.0, I fixed it with @BradZzz suggestion to:

Had to npm install inside node_modules/mongodb.

That can help. You might also want to try upgrading to a more recent version :)

I'm afraid I'll break my project. I'm happy with 3.8 working. :)

If it works, that's fine. Future bug fixes are gonna require upgrading to higher versions of 3.8.x though :)

I still have a problem with this on windows, using and npm3.
I'm on the latest version of mongoose (4.2.6)
Only way round I've found so is to npm install in the mongoose folder and then npm install in mongoose/node_modules/mongodb folder

@adrianw can you double check your version of mongoose and show me the output you get when you run it? It shouldn't be possible for you to get that error with 4.2.6, considering the fact that this particular require statement isn't in 4.2.6.

@vkarpov15 I was definitely using 4.2.6, I did some more digging last after your reply and discovered it was actually triggered by using the extension/plugin 'mongoose-3x-types' which I've now removed from my project and everything is just perfect again. So if anyone else is still having problems it's worth checking plugins

Saw this with v3.9.7, but rolling back to v3.8.12 seemed to work for now. Using latest NPM and node 4.2.4.

Don't use 3.9.7. It's deprecated in npm for a reason: it was an unstable dev release from back when mongoose used to use linux kernel version numbering rather than semver. Use 3.8.x or 4.x.

npm install [email protected]

@dhirender-tyagi what does that comment refer to?

Thanks @nottinhill, @nikola-kanacki - that resolved my issue (Error: Cannot find module './db.json'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17) )

I did this:
npm install -g node-gyp
cd /to/your/project-folder
rm -rf node_modules
npm install

This is still a bug in like every damn version of this package, you can't use it with Node 6.x, you can't use any version prior to 4.x without it breaking and showing this error, what is going on here? Why is there not documented Readme notes?

λ ~/Public/MyProject/ staging* node-gyp rebuild                                                                                                                         ∞
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | darwin | x64
gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR
gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR
gyp info spawn /usr/bin/python
gyp info spawn args [ '/Users/nexus/.nvm/versions/node/v5.10.1/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/nexus/Public/MyProject/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/nexus/.nvm/versions/node/v5.10.1/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/nexus/.node-gyp/5.10.1/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/nexus/.node-gyp/5.10.1',
gyp info spawn args   '-Dnode_gyp_dir=/Users/nexus/.nvm/versions/node/v5.10.1/lib/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/nexus/Public/MyProject',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp: binding.gyp not found (cwd: /Users/nexus/Public/MyProject) while trying to load binding.gyp
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/nexus/.nvm/versions/node/v5.10.1/lib/node_modules/node-gyp/lib/configure.js:305:16)
gyp ERR! stack     at emitTwo (events.js:100:13)
gyp ERR! stack     at ChildProcess.emit (events.js:185:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:204:12)
gyp ERR! System Darwin 15.5.0
gyp ERR! command "/Users/nexus/.nvm/versions/node/v5.10.1/bin/node" "/Users/nexus/.nvm/versions/node/v5.10.1/bin/node-gyp" "rebuild"
gyp ERR! cwd /Users/nexus/Public/MyProject
gyp ERR! node -v v5.10.1
gyp ERR! node-gyp -v v3.4.0
gyp ERR! not ok

Hi guys, just a heads up as I was having all of these issues and nothing seemed to work. I'm sure you all probably read up on the docs but I didn't and missed out the part where you shouldn't include node_modules in your git push to the heroku app.

This doc saved me

https://devcenter.heroku.com/articles/troubleshooting-node-deploys

Well that's fairly obvious. :) Even then though, this issue is ultra annoying. I don't see how a package like mongoose, which is used by thousands upon thousands of devs and projects, and backed by Automattic, can't get this shit sorted out. If there are strict requirements for versions, then PUT IT IN THE SHITTY DOCUMENTATION ON THE VERY FIRST PAGE.

Google searching this issue finds about 5 pages of results of people asking the same damned question! And there is no definitive answer!!!

Upgrading to the latest version of Mongoose and MongoDB worked for me. Though it did take some time to go through all the Mongoose changes in 4.x compared to 3.x!

Are there a lot of breaking changes? The shitty thing is that my mongoose dependency is baked into Keystone js, so I can't easily bump that version.

I have this issue with node 6.5.x , mongoose 4.4.x running Ubuntu 16, by switching to node 4.x it works. Question to you guys, now that node 6 will be the new LTS will it work? This seems a huge blocker if not fixed

@molekilla have you tried rm -rf node_modules && npm install ? And yes, mongoose works in node v6 just fine.

Hi.
Change to mongoose 4.2.9 in your package.json. it works for me.

i actually fix it with @BradZzz 's comment

I am getting below issue,
0 info it worked if it ends with ok
1 verbose cli [ 'C:\Program Files\nodejs\node.exe',
1 verbose cli 'D:\Users\e849278\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js',
1 verbose cli 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle [email protected]~prestart: [email protected]
6 info lifecycle [email protected]~start: [email protected]
7 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~start: PATH: D:Users\e849278\AppData\Roaming\npm\node_modules\npm\bin\node-gyp-bin;d:\nodejs\passport-social\node_modules.bin;C:\Program Files (x86)\Google\Chrome\Application;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\IBM\RationalSDLC\common;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\IBM\RationalSDLC\ClearCase\bin;D:Users\e849278.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Common Files\ThinPrint\;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\GitExtensions\;C:\Program Files\1E\NomadBranch\;C:\Program Files\nodejs\;C:\Ruby24-x64\bin;D:Users\e849278.dnx\runtimes\dnx-clr-win-x86.1.0.0-rc1-update1\bin;D:Users\e849278.dnx\bin;C:\Git\cmd;D:Users\e849278\AppData\Roaming\npm;C:\Program Files\CMake\bin;C:\Program Files\McAfee\DLP\Agent\
9 verbose lifecycle [email protected]~start: CWD: d:\nodejs\passport-social
10 silly lifecycle [email protected]~start: Args: [ '/d /s /c', 'node ./bin/www' ]
11 silly lifecycle [email protected]~start: Returned: code: 1 signal: null
12 info lifecycle [email protected]~start: Failed to exec start script
13 verbose stack Error: [email protected] start: node ./bin/www
13 verbose stack Exit status 1
13 verbose stack at EventEmitter. (D:Users\e849278\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\index.js:280:16)
13 verbose stack at emitTwo (events.js:126:13)
13 verbose stack at EventEmitter.emit (events.js:214:7)
13 verbose stack at ChildProcess. (D:Users\e849278\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack at emitTwo (events.js:126:13)
13 verbose stack at ChildProcess.emit (events.js:214:7)
13 verbose stack at maybeClose (internal/child_process.js:925:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid [email protected]
15 verbose cwd d:\nodejs\passport-social
16 verbose Windows_NT 6.1.7601
17 verbose argv "C:\Program Files\nodejs\node.exe" "D:\Users\e849278\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js" "start"
18 verbose node v8.9.3
19 verbose npm v5.5.1
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] start: node ./bin/www
22 error Exit status 1
23 error Failed at the [email protected] start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

@kalburgimanjunath I don't see mongoose anywhere in your stack trace, why is this related to mongoose?

@parth0595 it worked for me as well. Thanks

Was this page helpful?
0 / 5 - 0 ratings