Search code examples
vue.jselectronvite

Cannot Import local MongoDB/Mongoose file in Electron-Vite (Vue) project


I have an Electron project built with Vite, with MongoDB and Mongoose. However, I'm getting an error in my index.js file if I import from from a local file/module.

This is my index.js(entry point of Electron):

import { connect } from "./MongoDB";

The local file - MongoDB.js:

module.exports = {
  connect,}

The error I'm getting:

> electron-vite dev

vite v5.3.4 building SSR bundle for development...
✓ 3 modules transformed.
x Build failed in 66ms
 ERROR  src/main/index.js (5:9): "connect" is not exported by "src/main/MongoDB.js", imported by "src/main/index.js".


3: import { electronApp, optimizer, is } from "@electron-toolkit/utils";
4: import icon from "../../resources/icon.png?asset";
5: import { connect } from "./MongoDB";
            ^
6: // import mongoose from "mongoose";

These are the steps I took:

  1. Run npm create @quick-start/electron@latest
  2. select vue
  3. install mongoose and create a file MongoDB.js in same folder as index.js
  4. try to import a function

If I import mongoose directly in the index.js, it works fine, but I want to use the local file.

How can I fix this please?


Solution

  • Apparently CJS and Es6 import are different so i fixed it by export { connect }