yan-foto / electron-reload

Simplest (:pray:) way to reload an electron app on file changes!
https://www.npmjs.com/package/electron-reload
MIT License
521 stars 55 forks source link

Can't import (the CJS) electron-reload from ESM modules, due to use of deprecated CJS feature: module.parent #121

Open mnmkay opened 6 months ago

mnmkay commented 6 months ago

Here is electron-reload's source use of module.parent

Here is discussion about module.parent's problem with being imported, import(CJS)

Here is deprecation commit that suggests testing require.main

To test, run a single-line ESM file from node: TEST.mjs:

import 'electron-reload'

Output

TypeError: Cannot read properties of undefined (reading 'filename')

xcy960815 commented 6 months ago

I solve this problem like this:

import electronReloader from "electron-reloader";

if (process.env.NODE_ENV === "development") {
  try {
    module.path = __dirname;
    module.filename = __filename;

    electronReloader(module, { watchRenderer: true });
  } catch (err: any) {
    console.log(err);
  }
}

I don't know why (I think, webpack is causing this.) but when I run the program, the filename and dirname is not defined so I defined it hardly and it worked.

It's electron-reload, not electron-reloader. I have the same problem.

kursattkorkmazzz commented 6 months ago

I solve this problem like this:

import electronReloader from "electron-reloader";

if (process.env.NODE_ENV === "development") {
  try {
    module.path = __dirname;
    module.filename = __filename;

    electronReloader(module, { watchRenderer: true });
  } catch (err: any) {
    console.log(err);
  }
}

I don't know why (I think, webpack is causing this.) but when I run the program, the filename and dirname is not defined so I defined it hardly and it worked.

It's electron-reload, not electron-reloader. I have the same problem.

Oww you are right! Sorry guys :D I confused them.