I want to use Realm to write to my MongoDB database through a website. I have tested my code with a standalone TypeScript file, which imports realm as follows:
let {Realm} = require('realm');
Now, I am trying to integrate this into my website, and have the following in my “db.ts” file
import Realm from 'realm';
interface MongoDBDocument {
[key: string]: any;
}
export async function writeToMongoDB(document: MongoDBDocument, collectionName: string): Promise<any> {
const app = new Realm.App({ id: <MY_ID> });
const credentials = Realm.Credentials.anonymous();
try {
const user = await app.logIn(credentials);
const mongo = user.mongoClient("mongodb-atlas");
const collection = mongo.db("txinfo").collection(collectionName);
const res = await collection.insertOne(document);
return res;
} catch (err) {
console.error("Failed:", err);
throw err; // Rethrow the error to handle it in the calling function
}
}
However, when I run pnpm build
, I am getting the error:
Package path . is not exported from package <LOCAL_PATH>/node_modules/realm
. I am a newbie to all of this, but looking at *<LOCAL_PATH/node_modules/realm/package.json
, I see the following in exports:
"exports": {
".": {
"types": "./dist/public-types/index.d.ts",
"node": "./dist/platform/node/index.js",
"react-native": "./index.react-native.js"
},
"./experimental/base-url": {
"types": "./dist/public-types/experimental/base-url.d.ts",
"default": "./dist/experimental/base-url.js"
},
"./scripts/submit-analytics": "./scripts/submit-analytics.mjs",
"./react-native.config.js": "./react-native.config.js",
"./package.json": "./package.json"
},
It seems there is a “.” exported. Can someone please help me figure out how to import realm properly? I have also tried import {Realm} from '@realm/react'
, but same error