How to import MongoDB using es6 style import?

Hope this is a simple question. I am trying to import MongoDB using es6 import style. If I import using node, it should work fine.

let mongo = require('mongodb'); let MongoClient = mongo.MongoClient; 

But if I import it into es6, it breaks without errors or logs.

 import {MongoClient} from 'mongodb'; 

But it does not break when compiling / starting it only breaks when I try to do something with MongoClient.

Here is my Db Manager class -

 import {MongoClient} from 'mongodb'; export class DbManager { constructor() { console.log('Constructing DB Connection'); } } 

When I start 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 console.log MongoClient, there is simply no output.

 import {MongoClient} from 'mongodb'; export class DbManager { constructor() { console.log('Constructing DB Connection'); console.log(MongoClient); } } 

And the result is as follows:

 mycomputer myuser$ ./start.sh mycomputer myuser$ 

There are no compilation errors, so I don’t understand why this is not working. Also, I don't understand why there are no magazines! This is one of the last things that happen, at least there should be magazines until I think about it. If you want 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 the search for an answer, waiting for an answer (hoping). I am looking at the final.js result, and if MongoClient is used anywhere in the file, the call to the System.register function looks like this:

 System.register("db/db.manager", ["mongodb"] ... 

And if I do not use it (even if I import it), it does not show mongodb .

 System.register("db/db.manager", [] ... 

This explains why nothing will happen. Something is wrong with trying to import mongodb . Not sure what to do.

EDIT EDIT

Found a solution. Alone, I am not thrilled, but perhaps this is how it should be.

I don't think I can rely on es6 imports. It looks like I can use it to import typedefs, but not for the real module. How I got around this this way:

 import {Db as MongoDb, MongoClient} from 'mongodb'; let mongodb = require('mongodb'); let mongoClient: MongoClient = mongodb.MongoClient; 

Lots of extra work. If there is another way, please let me know.

+12
source share
2 answers
 import { MongoClient } from 'mongodb'; 

just imports a 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 value is equal

 let mongodb = require('mongodb'); 
+2
source

Try it:

 import { default as mongodb } from 'mongodb'; let MongoClient = mongodb.MongoClient; 
0
source

All Articles