MongoDB NodeJS driver's browserify compatibility (debugging help)

拜拜、爱过 提交于 2019-12-14 03:22:04

问题


Trying to figure out what's the problem that I can't browserify mongodb native nodejs driver. I'm assuming there is something wrong here in my setup as I tried googling if there was any known current compatibility problem with MongoDB NodeJS driver and browserify but nothing conclusive came up after couple days trying ;(

After running my code on browserify I get a

TypeError: require(...).native is not a function

I read on the driver's github repo that this driver depends on some native functionality and on kerberos and node-gyp. I installed the driver on my machine without any problem with npm install mongodb --save so I don't know if that might be the case.

I ran out of debugging ideas, so I'm turning to the blessed SO community :)

I'm developing for AWS Lambda using the Serverless Framework (so I'm concerned if the driver will actually run on a Lambda instance as the driver requires some native functionality...). Here is my testing code

var MongoClient = require('mongodb').MongoClient;
var x = "Hello NodeJS";

console.log(x);

MongoClient.connect("mongodb://localhost:27017/test", (err, db) => {
  console.log("Error null? " + (err == null ? "YES" : "NO"));
  console.log("DB null? " + (db == null ? "YES" : "NO"));
});

I was using some ES6 syntax like import and let bc in my original code I also transform it with babelify but removed, so just plain browserify creates some problem

I ran it like

browserify simple.js -o browserified.js

If someone can shed a light on this, I'd appreciate.

Thanks


回答1:


I created an issue at MongoDB JIRA server and I was confirmed the driver as of today doesn't run in the browser. Of course, I was not trying to run it in the browser, but in Node, but as my friend @dvlsg pointed in the comments of my question it is possible browserify shims some stuff important for the correct implementation of MongoDB driver. So if you're out there pulling your hair off as of today, try a workaround which does not include bundling up mongodb driver with browserify.

The driver maintainer confirmed he was working on a version that will work in the browser, which will possibly work the way I needed but it is not ready even for alpha release, hopefully if you're reading this in the future...



来源:https://stackoverflow.com/questions/36902720/mongodb-nodejs-drivers-browserify-compatibility-debugging-help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!