How to import MongoDB using es6 style imports? - javascript

Hopefully this is a simple question. I am trying to import MongoDB using the es6 import-from style. If I import using node require it works fine.
let mongo = require('mongodb');
let MongoClient = mongo.MongoClient;
But if I import it the es6 way it breaks without errors or logs.
import {MongoClient} from 'mongodb';
But it doesn't break when compiling/running it only breaks when I try to do anything with MongoClient.
Here is my Db Manager class-
import {MongoClient} from 'mongodb';
export class DbManager {
constructor() {
console.log('Constructing DB Connection');
}
}
When I run my server I get several logs from other managers and events.
mycomputer myuser$ ./start.sh
Server Constructing
Route Manager Constructing
Initializing Route: Static
Constructing DB Connection
http server started on port: 8000
But if I do a console.log of the MongoClient there is simply no output.
import {MongoClient} from 'mongodb';
export class DbManager {
constructor() {
console.log('Constructing DB Connection');
console.log(MongoClient);
}
}
And the output looks like this-
mycomputer myuser$ ./start.sh
mycomputer myuser$
There are no compile errors so I don't understand why this isn't working. Furthermore, I don't understand why there aren't any logs! This is one of the last things that happens, there should at least be logs up until that point I'd think. If you'd like to see my start.sh script here it is (quick and dirty, don't judge me):
tsc
echo "var System = require('systemjs');" > dist/final.js
babel dist/typescript.js >> dist/final.js
echo "System.import('main');" >> dist/final.js
node dist/final.js
EDIT
Continuing to search for the answer while waiting (hoping) for a response. I'm taking a look at the resulting final.js and if MongoClient is used anywhere in the file the System.register function call looks like this-
System.register("db/db.manager", ["mongodb"] ...
And if I don't use it (even if I import it) it does not show mongodb.
System.register("db/db.manager", [] ...
That would explain why nothing would happen. Something is wrong with trying to import mongodb. Not sure yet what to do.
EDIT EDIT
Found a solution. One i'm not thrilled with but maybe it's just the way it has to be.
I don't think I can rely on es6 imports. It looks like I can use it to import the typedefs but not the actual module. How I got around this is like this-
import {Db as MongoDb, MongoClient} from 'mongodb';
let mongodb = require('mongodb');
let mongoClient: MongoClient = mongodb.MongoClient;
A lot of extra work. If there's another way please let me know.

Listen, I know there are more than a handful of cracks at this solution here. Some may work for you, but for me, none solved me but the one below.
2021 UPDATE:
BORING BACKSTORY ALERT
We're using Node v14.16.0 and our package.json has "type": "module" set. So, our code is ES6+ and commonjs imports are a deal-breaker in most cases, especially when it comes to the MongoDB Native 3.6 NodeJS Driver.
Lucky for us, MongoDB Native ^4.0.0-beta.3 is written in TypeScript, and works as expected. Prepare your shattered sprits for liftoff. ;) Oh, and rather than store your secret sauce (user:pass and uri) in your source code, check out node-config and be safe.
THE SOLUTION
# This version will keep changing after this posts, so take heed.
$ cd path/to/your/project
$ npm i -s mongodb#4.0.0-beta.3
Inside your project:
import config from 'config'
// MongoDB
import { MongoClient } from 'mongodb'
const client = new MongoClient(config.get('mongodb.uri'))
await client.connect()
const db = client.db()
const stuff = db.collection('AllTheStuff')
const record = {
type: "ThisAndThat",
lastUpdated: new Date().getTime()
}
const query = { type: "ThisAndThat" }
const options = { upsert: true }
const result = await stuff.replaceOne(query, record, options)
And now all your cats are sleep silent tonight. Hopefully this lowers the level of unchallenged insanity in the world, or helps you in your quest, whichever suits your taste in achievement. :)

import { MongoClient } from 'mongodb';
just imports type definition from node_modules/#types/mongodb/index.d.ts
import * as mongodb from 'mongodb';
imports everything from node_modules/mongodb/index.js and its the same as
let mongodb = require('mongodb');

Try this:
import { default as mongodb } from 'mongodb';
let MongoClient = mongodb.MongoClient;

Edit
As Elihu pointed out in the comment below, the additional integration of #types/mongo is now deprecated and is no longer needed as the mongo-package now comes with types per default.
npm install --save mongodb is therefore sufficient.
Original Answer
As mkalmo suggested you can import the mongodb types:
Step 1: Install via the npm mongodb types package
npm install --save #types/mongodb
Step 2: Use it!
import mongodb from "mongodb";
const MongoClient = mongodb.MongoClient;

This works for me:
import mongodb from 'mongodb'
const { MongoClient } = mongodb

Related

Package path ./standalone is not exported from package

I'm trying to use the firebase admin SDK, heres my code:
import * as admin from 'firebase-admin';
var firebaseAdminAccount = require("../serviceAccount.json");
var app : admin.app.App = null;
if(!admin.apps.length)
{
app = admin.initializeApp({
credential: admin.credential.cert(firebaseAdminAccount)
})
}
if(app === null)
{
app = admin.apps[0];
}
export default app;
the idea behind this is that whenever used, it will check if the firebase admin SDK is initialized or not, if it's not, then it will initialize it, then export it.
My problem however is when I try to run this, it gives me the following error:
error -
./node_modules/firebase-admin/lib/app/firebase-namespace.js:106:0
Module not found: Package path ./standalone is not exported from
package D:\NewRepos\1d3a\node_modules#firebase\database-compat (see
exports field in
D:\NewRepos\1d3a\node_modules#firebase\database-compat\package.json)
Import trace for requested module:
./node_modules/firebase-admin/lib/default-namespace.js
./node_modules/firebase-admin/lib/index.js ./lib/firebaseAdminSdk.ts
./middleware.ts
https://nextjs.org/docs/messages/module-not-found\
I just installed everything so it should be on the latest version, anyone have an idea why this is happening?
Seems like I didn't realize that Next.js middleware runs on V8, and therefor, firebase-admin cannot run on it. Back to the drawing board.

Connecting Redis microservice in NestJS causes app to get stuck

I'm trying to set up a simple hybrid app using Nest's documentation, but the app gets stuck without throwing.
main.ts
import { NestFactory } from '#nestjs/core';
import { AppModule } from './app.module';
import { Logger } from '#nestjs/common';
import { ConfigService } from '#nestjs/config';
import { MicroserviceOptions, Transport } from '#nestjs/microservices';
const logger = new Logger('Main');
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
const redisConfig = configService.get('database.redis');
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.REDIS,
options: {
url: `redis://${redisConfig.host}:${redisConfig.port}`,
},
});
await app.startAllMicroservices();
await app.listen(configService.get('app.port'));
}
bootstrap()
.then(() => logger.log('App running'))
.catch((e) => logger.error(e));
When I comment out app.startAllMicroservices() or the code connecting the microservice, the App running line is logged, with it, the app is stuck.
I am 100% certain Redis is up and running and responsive, I am using Bull which uses the same config and it runs just fine.
I have tried commenting out everything irrelevant to the above (everything besides the ConfigModule) in the app.module to no avail. Any help would be appreciated.
I am running the latest version of NestJS and its peer dependencies.
Just resolved a similar issue. As per the time of writing downgrade redis npm package to ^3 from anything high i.e ^4.
From nestjs microservice redis docs To start building Redis-based microservices, first install the required package (note as of now the supported Redis version is ^3, not the latest ^4):

Change JavaScript in TypeScript

I would like to translate this code into actual TypeScript. Could you help me there?
import mongoose, { Promise } from 'mongoose';
Promise = global.Promise;
const db = {};
db.mongoose = mongoose;
db.user = require("./user.model");
db.role = require("./role.model");
db.ROLES = ["user", "admin", "moderator"];
export default db;
It would be great if you'd provide us with what you have tried and achieved so that it is easier to modify your code.
A way to do this would be to type this in a TypeScript file in an editor that provides linting for TypeScript like Visual Studio Code. Then check all the errors and fix them.

Property 'connect' does not exist on type 'typeof import

I have problem when I try to use mongodb.connect() func..
import mongodb from "mongodb"
import dotenv from "dotenv"
dotenv.config()
mongodb.connect()
Then error appears " Property 'connect' does not exist on type 'typeof import(..)
To create a connection to mongodb, you should first import the MongoClient, then you need to create a instance of the client, and only through the instance you will be able to create the connection.
See the example bellow taken from mongodb package documentation in npm
import { MongoClient } from 'mongodb'
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
await client.connect();
Check this out for more details.
The connect functions exits inside mongoose package.
Look into this link for more info [https://www.npmjs.com/package/mongoose]

How to correct: SyntaxError: Named export 'mongo' not found

Trying to run a simple script to create a user with mongoose using nodejs. So basically just running node my_script.js which is the official way as per Run Node.js scripts from the command line
I have tried importing in the following ways:
import Connection from 'mongoose';
const { mongo } = Connection;
import mongoose from "mongoose"
import mongoose from "node_modules/mongoose/index.js"
const mongoose = require("mongoose")
Also tried adding the following lines to package.json:
"type": "module",
"esModuleInterop": true,
Complete error seen below for most of the import cases above:
import mongoose, { mongo } from "mongoose";
^^^^^
SyntaxError: Named export 'mongo' not found. The requested module 'mongoose' is a CommonJS module, which may not support all module.exports as named exports.CommonJS modules can always be imported via the default export, for example using:
import pkg from 'mongoose';
const { mongo } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:143:5)
at async Loader.import (internal/modules/esm/loader.js:165:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
From mongoose index.js file I do not see any other export besides:
const mongoose = module.exports = exports = new Mongoose({
[defaultMongooseSymbol]: true
});
Found this github issue Misleading error that module does not provide export
#32137 but still not sure how to make this import to work.
This "appears" to be the fix which is the only thing that happened after I took a break.
I had 2 files: B.js and A.js
from A.js:
import foo from "./B.js";
import mongoose from "mongoose";
I decided to run B.js on it's own to see if it would give me the same error(it had a mongoose import as well), it turn's out there was another different error:
foo.pre(save, function(next) {
^ ReferenceError: save is not defined
Ok nothing biggy, corrected it and node B.js worked fine when running it, hence decided to test with node A.js, which worked just fine to my surprise!
This turned out to be completely counterintuitive, as the error mentioned initially was not pointing to this.

Categories

Resources