Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting "UnhandledPromiseRejectionWarning: ReferenceError: __filename is not defined" when trying to open DB in electron project #1818

Closed
XunJiJiang opened this issue Jan 23, 2025 · 1 comment

Comments

@XunJiJiang
Copy link

Issue Summary

An error occurred when creating a new database connection using the Database constructor.

After troubleshooting, the error occurs in the Database constructor in the following file (even if no Database instance is created).

The error does not occur when the contents of the Database constructor in this file are removed.

sqlite.ts

import { app } from 'electron'
import { join } from 'node:path'
import sq from 'sqlite3'

class Database {
  private db: sq.Database

  constructor() {
    // delete here 
    this.db = new sq.Database(dbPath, (err) => {
      if (err) {
        console.error('Error opening database:', err)
      }
    })
  }

}

const userDataPath = app.getPath('userData')
const dbPath = join(userDataPath, 'sqliteDatabase.db')

const useSqlite = () => {}

export default useSqlite

package.json

{
  "name": "desktop-tools",
  "private": true,
  "version": "0.0.1",
  "description": "",
  "author": "xunji",
  "type": "module",
  "main": "dist-electron/main.js",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc && vite build && electron-builder",
    "preview": "vite preview"
  },
  "dependencies": {
    "pinia": "^2.3.1",
    "sqlite3": "^5.1.7",
    "vue": "^3.5.13",
    "vue-i18n": "^11.0.1",
    "vue-router": "^4.5.0"
  },
  "devDependencies": {
    "@tsconfig/node22": "^22.0.0",
    "@types/jsdom": "^21.1.7",
    "@types/node": "^22.10.7",
    "@vitejs/plugin-vue": "^5.2.1",
    "@vitest/eslint-plugin": "1.1.25",
    "@vue/eslint-config-prettier": "^10.2.0",
    "@vue/eslint-config-typescript": "^14.3.0",
    "@vue/test-utils": "^2.4.6",
    "@vue/tsconfig": "^0.7.0",
    "electron": "^34.0.0",
    "electron-builder": "^25.1.8",
    "eslint": "^9.18.0",
    "eslint-plugin-vue": "^9.32.0",
    "jsdom": "^26.0.0",
    "npm-run-all2": "^7.0.2",
    "prettier": "^3.4.2",
    "sass-embedded": "^1.83.4",
    "typescript": "^5.7.3",
    "vite": "^6.0.11",
    "vite-plugin-electron": "^0.29.0",
    "vite-plugin-electron-renderer": "^0.14.6",
    "vite-plugin-static-copy": "^2.2.0",
    "vite-plugin-vue-devtools": "^7.7.0",
    "vitest": "^3.0.3",
    "vue-tsc": "^2.2.0"
  }
}

Relevant logs or output

App threw an error during load
(node:223) UnhandledPromiseRejectionWarning: ReferenceError: __filename is not defined
(Use Electron --trace-warnings ... to show where the warning was created)
(node:223) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Version

5.1.7

Node.js Version

22.13.0

How did you install the library?

macOS 15.2, electron 34.0

@XunJiJiang
Copy link
Author

This may be because vite packages the main process code into a single es module file, causing __filename to not exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant