NPM MongoDB installation fails on Windows 7 with compilation errors

I’m having trouble setting up MongoDB through NPM on my Windows 7 machine. Even though I have Python and Visual C++ installed, the installation keeps failing with a compilation error.

Here’s the error I’m getting:

D:\projects\webapp\server>npm install mongodb
npm WARN package.json [email protected] No README.md file found!
npm WARN package.json [email protected] No README.md file found!
npm http GET https://registry.npmjs.org/mongodb
npm http 304 https://registry.npmjs.org/mongodb
npm http GET https://registry.npmjs.org/bson/0.1.5
npm http 304 https://registry.npmjs.org/bson/0.1.5
npm WARN package.json [email protected] No README.md file found!

> [email protected] install D:\projects\webapp\server\node_modules\mongodb\node_modules\bson
> node-gyp rebuild || (exit 0)

D:\projects\webapp\server\node_modules\mongodb\node_modules\bson>node "D:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild
  bson.cc
..\ext\bson.cc(8): fatal error C1083: Cannot open include file: 'alloca.h': No such file or directory [D:\projects\webapp\server\node_modules\mongodb\node_modules\bson\build\bson.vcxproj]

gyp ERR! build error
gyp ERR! stack Error: `D:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (D:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:219:23)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:91:17)
gyp ERR! stack     at Process._handle.onexit (child_process.js:674:10)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "D:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd D:\projects\webapp\server\node_modules\mongodb\node_modules\bson
gyp ERR! node -v v0.8.8
gyp ERR! node-gyp -v v0.6.7
gyp ERR! not ok
[email protected] node_modules\mongodb
└── [email protected]

The error seems to be related to a missing ‘alloca.h’ file during the BSON compilation process. Has anyone encountered this issue before and found a solution?

Thanks in advance!

This is a common challenge with MongoDB on Windows due to the absence of the ‘alloca.h’ file, which is typically found in Unix environments. To resolve this, consider upgrading to a newer version of the MongoDB driver with the command npm install [email protected]. If upgrading isn’t feasible, you can bypass the compilation step by setting the environment variable set npm_config_bson_ext_ignore_build_failures=true before running your npm install. This will allow you to proceed with a JavaScript fallback, which generally performs adequately.

Had this exact problem two years ago on a legacy project. The alloca.h header issue on Windows is super annoying. I fixed it by installing Windows SDK 7.1 with Visual Studio 2010 Express - it gives you the missing headers that node-gyp needs. Just grab it from Microsoft’s site. After installing, use the SDK command prompt to set your environment variables properly, then try npm install again. Having both the SDK and Visual Studio components together fixed all my missing include errors. Plus you get full native BSON performance instead of some workaround.

downgrade to node 0.10.x - i hit the same problem with that old bson version. newer node versions dont work with it. also check you’ve got the right visual studio installed. node-gyp is picky about which c++ compiler it’ll work with.