'Module declares 'MongoCLient' locally, but it is not exported
I have an API that has imported the MongoClient module:
import { MongoClient } from 'mongodb';
but I am getting an error when I go to run the api saying Module '"../../node_modules/mongodb/mongodb.ts34"' declares 'MongoClient' locally, bit it is not exported.
I do not recall if I ever worked with the mongodb library, but I looked at their documentation on npm repo and I also thought perhaps I was missing an npm install, but I am still getting this error.
A type definition file does exist for it inside the MongoDB library and I do see export declare class MongoClient extends TypedEventEmitter<MongoClientEvents>, so I remain unclear as to where the error is indicated that this module is not being exported.
I think the mongodb library was added because someone was following the instructions here:
https://www.mongodb.com/docs/drivers/node/current/
Based on this documentation:
https://www.mongodb.com/compatibility/using-typescript-with-mongodb-tutorial
I changed the import statement to:
import * as mongoDB from 'mongodb';
The codebase itself looks like so:
import mongoose from 'mongoose'
// import { MongoClient } from 'mongodb'
import * as mongoDB from 'mongodb';
import fetch from 'isomorphic-fetch'
import {
EmrPatient,
EmrDoctor,
EmrPatientModel,
EmrDoctorModel,
} from '../models/Emr'
import { logger } from '../logger/winston-logger'
// Move this to config
const MONGO_ATLAS_HOST: string = 'spirit-dev-abcdefg.mongodb.net'
const MONGO_LOGIN: string = 'spirit-server'
const DB_NAME: string = 'dev'
const LOGIN_PATH: string = '/auth/login'
const ACCOUNT_PATH: string = '/api/v1/account/'
let gSpyrtHome: string | null = null
let gSpyrtJwt: string | null = null
let gMongoClient = null
let gDatabase: any = null
const {
NODE_ENV,
USE_MONGO_ATLAS,
V3_MONGO_PASSWD,
SPYRT_ADMIN_EMAIL,
SPYRT_ADMIN_PASSWORD,
} = process.env
function mongoConnectionURI(): string {
let mongoURI: string = ''
if (NODE_ENV === 'local' && !USE_MONGO_ATLAS) {
mongoURI = 'mongodb://' + 'localhost:21111' + '/' + DB_NAME
} else {
const mongoPassword: string | undefined = V3_MONGO_PASSWD
mongoURI = `mongodb+srv://${MONGO_LOGIN}:${mongoPassword}@${MONGO_ATLAS_HOST}/${DB_NAME}?retryWrites=true&w=majority`
}
return mongoURI
}
function connectToMongo(): void {
const mongoURI: string = mongoConnectionURI()
const options = {}
mongoose.connect(mongoURI, options).catch((err) => {
logger.info('Mongo connection failed!', err)
})
mongoose.connection.on('error', (err) => {
logger.info('Mongo error!', err)
})
mongoose.connection.once('open', () => {
logger.info('Successfully connected to Mongo')
})
}
async function connectToMongoDirect(): Promise<void> {
const mongoURI: string = mongoConnectionURI()
gMongoClient = new mongoDB.MongoClient(mongoURI)
await gMongoClient.connect().catch((err) => {
logger.info('Mongo Client connection error!', err)
})
gDatabase = await gMongoClient.db('dev')
if (gDatabase != null) {
logger.info('Successfully connected to Mongo Client')
} else {
logger.info('Cannot access Mongo Database dev!')
}
}
Now it's complaining with the following:
Property 'MongoClient' does not exist on type 'typeof import'
gMongoClient = new mongoDB.MongoClient(mongoURI)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
