gpt4 book ai didi

node.js - 无法加载 c++ bson 扩展,使用纯 JS 版本——没有 Mongoose ,没有 express ,只有 Node 驱动程序

转载 作者:可可西里 更新时间:2023-11-01 09:44:22 25 4
gpt4 key购买 nike

我完成了一个非常简单的 mongodb 教程,其中我们使用 mongo shell 创建了一个数据库,创建了一个集合,然后编写了一个非常简单的 nodejs 程序来访问集合并将文档记录到控制台。

var mongodb = require('mongodb');

var db = new mongodb.Db('mcfly', new mongodb.Server('127.0.0.1', 27017), {safe: true});

db.open(function(err) {
db.collection('mathpeeps', function(err, collection){
collection.find().toArray(function(err, items){
console.log(items)
});
});
});

但是当我运行程序时,它会给我:

js-bson: Failed to load c++ bson extension, using pure JS version
[ { _id: 53ab29bbdb49942ae25b5201,
firstname: 'Leonard',
lastname: 'Euler',
born: 1707 },
{ _id: 53ab29fedb49942ae25b5202,
firstname: 'Karl',
lastname: 'Gauss' } ]

我在 SO 上搜索并搜索了“js-bson:无法加载 c++ bson 扩展,使用纯 JS 版本”并尝试了所有方法,除了对其他人不起作用的答案或围绕 mongoose 问题的答案我在这个练习中没有使用 Mongoose 。

到目前为止我已经尝试过的一些事情:

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

(但本练习甚至不使用 package.json 文件)


我尝试用最新版本重新安装 node-mongodb


我试过了

npm install -g node-gyp

我在 SO 这里尝试了一个答案 Failed to load c++ bson extension :

进入 node_modules/mongodb/node_modules/bson 目录并从那里使用

node-gyp rebuild

然后产生:

gyp info it worked if it ends with ok
gyp info using node-gyp@0.13.1
gyp info using node@0.11.9-pre | linux | x64
gyp ERR! configure error
gyp ERR! stack Error: "pre" versions of node cannot be installed, use the --nodedir flag instead
gyp ERR! stack at install (/usr/local/lib/node_modules/node- gyp/lib/install.js:66:16)
gyp ERR! stack at Object.self.commands.(anonymous function) [as install] (/usr/local/lib/node_modules/node-gyp/lib/node-gyp.js:66:37)
gyp ERR! stack at getNodeDir (/usr/local/lib/node_modules/node- gyp/lib/configure.js:152:20)
gyp ERR! stack at /usr/local/lib/node_modules/node-gyp/lib/configure.js:95:9
gyp ERR! stack at ChildProcess.exithandler (child_process.js:659:7)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:106:17)
gyp ERR! stack at maybeClose (child_process.js:773:16)
gyp ERR! stack at Socket.<anonymous> (child_process.js:986:11)
gyp ERR! stack at Socket.EventEmitter.emit (events.js:103:17)
gyp ERR! stack at Pipe.close (net.js:458:12)
gyp ERR! System Linux 3.13.0-29-generic
gyp ERR! command "node" "/usr/local/bin/node-gyp" "rebuild"
gyp ERR! cwd /home/nathaniel/node_modules/mongodb/node_modules/bson
gyp ERR! node -v v0.11.9-pre
gyp ERR! node-gyp -v v0.13.1
gyp ERR! not ok

我在这里看到了一个 SO 答案 Failed to load c++ bson extension, using pure JS version说:

“听起来你在使用 npm 安装 bson 模块时没有安装 gcc/g++/make/python 2.x。尝试先安装它们,然后使用 npm 重新安装 bson。”

但是这个答案没有被选为解决方案,我不知道这意味着什么,因为我是一个严肃的菜鸟。


我真的很想继续学习 mongodb,但我不希望一些包 bug 导致 future 出现问题,让我错误地认为我做错了什么。我真的很想现在就把它弄清楚。

我还注意到许多发布此问题的其他人(没有 mongoose 或没有明确参与)没有选择答案或回复说他们解决了这个问题。

我正在运行 Ubuntu 14.04 LTS 和 Node v0.11.9-pre

任何帮助将不胜感激!!!!

最佳答案

我认为错误非常明确:错误:无法安装 Node 的“pre”版本,请改用 --nodedir 标志。因此,您应该提供从安装 Node 的根 Node 源目录的路径,或者安装/使用实际 Node 版本(v0.11.13 是最新的不稳定版本,v0.10.29 是最新的稳定版本在撰写本文时发布)并再次尝试 npm install/node-gyp rebuild

它要求 Node 源目录的原因是因为 node-gyp 必须从 nodejs.org 站点下载 Node 源 tarball 才能编译插件(它需要 C++ 头文件等)。但是,如果您有一个预发布的 Node 版本,node-gyp 无法知道要下载什么源代码树以匹配您已安装的内容。

关于node.js - 无法加载 c++ bson 扩展,使用纯 JS 版本——没有 Mongoose ,没有 express ,只有 Node 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24440768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com