bundle.js bundle.js:1 Uncaught Error: Cannot find module '/node_modules\mongodb-core\lib\topologies/../../package.json'

bundle.js bundle.js:1 Uncaught Error: Cannot find module '/node_modules\mongodb-core\lib\topologies/../../package.json'

我收到以下错误:

bundle.js:1 Uncaught Error: Cannot find module '/node_modules\mongodb-ore\lib\topologies/../../package.json'

这是我的版本详情:

OS: Windows10

MongoDB: 2.2.16

MongoDB-核心:2.1.2

节点:6.9.2

我已经使用了npm install bson-ext并将\node_modules\mongodb-core\node_modules\bson-ext\ext\index.js更改为

try {
// Load the precompiled win32 binary
if(process.platform == "win32" && process.arch == "x64") {
  bson = require('bson');
} else if(process.platform == "win32" && process.arch == "ia32") {
  bson = require('bson');
} else {
  bson = require('bson');
}
} catch(err) {
    console.log(err)
    // Attempt to load the release bson version
    try {
        bson = require('bindings')('bson.node');
    } catch (err) {
        throw new Error("js-bson: Failed to load c++ bson extension, using       pure JS version");
    }
}

而原来的是:

bson = require('./win32/x64/bson');  

因为当我尝试 browserify range.js > bundle.js 时,它无法在 mongoDB-core 中找到 bson-ext module

我不确定这样的操作会不会导致上面的错误。

这是我的 package.json 文件:

"dependencies": {
"browserify": "^13.1.1",
"bson": "^1.0.1",
"d3": "^4.4.0",
"express": "^4.14.0",
"hbs": "^4.0.1",
"jsdom": "^9.9.1",
"mongodb": "^2.2.16",
"mongodb-core": "^2.1.2"
}

我还不能证实这一点,但我认为问题在于 MongoDB 的 JavaScript (Node.js) 驱动程序不适用于浏览器,出于安全原因。不清楚 OP 中的问题是由于 Browserify 错误地解析了相对路径还是其他原因,但不管首选技术是通过节点服务器将请求代理到您的 MongoDB 实例。

Mongo 在此处列出了现成的解决方案: https://docs.mongodb.com/ecosystem/tools/http-interfaces/

另请注意 --rest 选项,它允许应用程序通过 URL 模式直接从数据库中读取: https://docs.mongodb.com/ecosystem/tools/http-interfaces/#simple-rest-api

如文档所述,出于安全考虑,这不是好的做法,但可能有助于原型设计。