【问题标题】:node.js tutorial failing because of missing bson modulenode.js 教程因缺少 bson 模块而失败
【发布时间】:2016-08-02 14:43:02
【问题描述】:

背景信息

我正在尝试遵循本教程: http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/

我有一个运行在另一个 linux 机器上的 mongo 数据库......而且,我知道它正在工作,因为我有一个可以连接和查询的 php 应用程序。

我没有使用猫鼬...而是僧侣,因为这是教程所要求的。

这是我的 package.json 文件的样子:

{
  "name": "testapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "mongodb": "^1.4.4",
    "monk": "^1.0.1",
    "morgan": "~1.6.1",
    "pug": "^2.0.0-beta4",
    "serve-favicon": "~2.3.0"
  }
}

~

问题

当我尝试运行测试节点应用程序时,我收到以下错误消息:

me@mydevbox:/var/www/html/node/testapp$ node app.js 
{ Error: Cannot find module '../build/Release/bson'
    at Function.Module._resolveFilename (module.js:440:15)
    at Function.Module._load (module.js:388:25)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/html/node/testapp/node_modules/mongodb/node_modules/bson/ext/index.js:15:10)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3) code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version

这是 index.js 文件的样子:

  3 try {
  4         // Load the precompiled win32 binary
  5         if(process.platform == "win32" && process.arch == "x64") {
  6           bson = require('./win32/x64/bson');  
  7         } else if(process.platform == "win32" && process.arch == "ia32") {
  8           bson = require('./win32/ia32/bson');
  9         } else { 
 10           bson = require('../build/Release/bson');
 11         }     
 12 } catch(err) {
 13         // Attempt to load the release bson version
 14         try {
 15                 bson = require('../build/Release/bson');
 16         } catch (err) {
 17                 console.dir(err)
 18                 console.error("js-bson: Failed to load c++ bson extension, using pure JS version");
 19                 bson = require('../lib/bson/bson');
 20         }
 21 }

我的尝试:

根据 stackoverflow.com 上的其他帖子,我尝试将第 15 行更改为如下所示:

15                 bson = require('bson');

我也试过运行命令:

 sudo npm install bson 

但这并没有解决问题。

如果有帮助,我在我的盒子上搜索了“bson”并得出了以下结果:http://pastebin.com/WKQygGak

系统信息

Ubuntu 15.10

编辑 1

me@mydevbox:/var/www/html/node/testapp$ sudo apt-get install gcc make build-essential
Reading package lists... Done
Building dependency tree       
Reading state information... Done
build-essential is already the newest version.
gcc is already the newest version.
make is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

然后我运行了以下命令:

http://pastebin.com/MDerppWB

第 10 行似乎有一个错误,但我不知道如何更正它。

编辑 2

这是我目前在我的 npm 配置中拥有的:

me@mydevbox:/var/www/html/node/testapp$ npm config list
; cli configs
user-agent = "npm/3.10.3 node/v6.3.1 linux x64"

; userconfig /home/me/.npmrc
https-proxy = "http://10.1.1.11:8080/"
proxy = "http://10.1.1.11:8080/"
python = "python2.7"

; globalconfig /usr/etc/npmrc

; node bin location = /usr/bin/nodejs
; cwd = /var/www/html/node/testapp
; HOME = /home/me
; "npm config ls -l" to show all defaults.

me@mydevbox:/var/www/html/node/testapp$ 

另外作为一个测试,我尝试“sudo npm install bson”,但它失败了,我在我发布的 pastebin 中看到了同样的错误。

【问题讨论】:

    标签: javascript node.js mongodb bson monk


    【解决方案1】:

    您可能需要构建必需品,因为这里有一些 c++ 依赖项。请参考following answer 或执行以下操作。

    sudo apt-get install gcc make build-essential
    
    rm -rf node_modules
    npm cache clean
    npm install
    

    编辑:

    这也可能是 python 问题。确保在 PATH 中为 ubuntu 安装了 python 2.7,然后设置 npm 以查找 python:

    npm config set python python2.7
    

    Others 遇到的问题与您发现的解决方案相同。 Python 看起来是下一步,因为您已经确定自己拥有构建要素。

    编辑 2:

    旁注.. 我知道您正在学习教程.. 但为了将来参考,我强烈建议使用native mongo driver。它比 mongoose 之类的 ORM 更快,并且不难使用(不是说 mongoose 很难使用,只是没有那么灵活或高性能)。

    【讨论】:

    • 嗨。请参阅编辑 1 了解您的答案的详细结果
    • 你好。是的...我之前已经尝试过...我已经为您发布了我的配置。请参阅编辑 2。
    • @Happydevdays 在你的路径中是 python 吗?您可以通过键入 python 在终端中使用路径?
    • 我认为这是因为我可以在命令行的任何位置输入“python”,它会触发 python 2.7。
    猜你喜欢
    • 1970-01-01
    • 2015-09-27
    • 2019-11-09
    • 2021-09-17
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多