dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.66k stars 641 forks source link

ReferenceError: self is not defined #1596

Open aspeculat0r opened 2 years ago

aspeculat0r commented 2 years ago

I am using dexie-export-import in svelte-kit. When I have a statement import "dexie-export-import" I get a reference error. image

image

dfahlander commented 2 years ago

dexie-export-import is a client-side library so you'd need to avoid it being executed on the server.

DjakaTechnology commented 1 year ago

I also got this error when building for client side tauri in NextJs

ReferenceError: self is not defined
    at C:\Projects\raita-desktop\raita-web\front-end\node_modules\dexie-export-import\dist\dexie-export-import.js:2022:54
    at C:\Projects\raita-desktop\raita-web\front-end\node_modules\dexie-export-import\dist\dexie-export-import.js:20:68
    at Object.<anonymous> (C:\Projects\raita-desktop\raita-web\front-end\node_modules\dexie-export-import\dist\dexie-export-import.js:23:3)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.7150 (C:\Projects\raita-desktop\raita-web\front-end\.next\server\pages\app\components\settings\database_export_import.js:18:18) {
  type: 'ReferenceError'
}
dfahlander commented 1 year ago

@DjakaTechnology NextJS will compile all your components under pages for both frontend and backend (node-based). The dexie-export-import addon probably fails when required from the backend because it was written for frontend.

We used to have similar problem with dexie-cloud-addon and at that time, we encouraged users to use dymanic import of components that depended on it. Now, this was fixed in dexie-cloud-addon but may still be an issue in dexie-export-import. To work around, use dynamic import to import any component that depend the failing module. Dynamic import in nextjs makes the code only execute client side.

fev4 commented 1 year ago

If anyone else finds themselves with this problem, give this a try:

  onMount(async () => {
    await import('dexie-export-import');
  });

It's working on my end.

ZeeshanAhmadKhalil commented 6 months ago

If anyone else finds themselves with this problem, give this a try:

  onMount(async () => {
    await import('dexie-export-import');
  });

It's working on my end.

where onMount is coming from?

ZeeshanAhmadKhalil commented 6 months ago

the following code worked for me in NextJS

import { db } from './connection';

const exportDB = async () => {
  if (typeof window !== 'undefined') {
    await import('dexie-export-import');
  }

  const blob = await db.export();
  console.log('blob===>', blob);
};

export { exportDB };