Uncaught Exception: Cannot find module ../package.json
marcoancona opened this issue · 8 comments
NOTE: before openning an issue, please consult previous issues (both open and closed).
What is the problem
The module raises an exception:
Uncaught Exception:
Error: Cannot find module '/home/myuser/projects/myproject/main/package.json'
How to reproduce
Include require("electron-reload")(app.getAppPath() + "../renderer")
in the entrypoint of the program.
Additional information
This happens with Electron 5.0.6
Apparently, getAppPath() points to the entry point of the program, which in this case is /home/myuser/projects/myproject/main/main.js
. The problem is that package.json
is in /home/myuser/projects/myproject/
not in /home/myuser/projects/myproject/main
. I see no way of specifying the relative path of package.json
with respect to getAppPath()
You can use something like path.join(__dirname, '..')
to access files from the location of you entry point.
The problem is not on the user side, but on the library side, as it crashes executing the following line when the module is imported
Line 10 in 6ad1a18
It requires a non-existing file based on the (not-necessarily true) assumption that
package.json
lies in the same folder of the program entry-point.It may differ between the operation systems but getAppPath()
should point to the location from where you are starting the app. If you are starting it with electron .
(and not electron ./main/main.js
) from the root of you project then it will find the package.json
I cannot reproduce the problem without knowing how your project is structured. Usually you define the entry point in the package.json
and start the electron by pointing it to the directory including the package.json
. Doing so would result in getAppPath()
returning the directory in which the package.json
is also located.
I think we can simplify the application logic by removing
Line 10 in 6ad1a18
Line 11 in 6ad1a18
const mainFile = __filename
It should work as electron-reload
is always initiated by the main file. @marcoancona could you give it a try and let me know? Thanks.
Yes, using const mainFile = __filename
works for me. Alternatively, one could pass the path of mainFile
optionally? This would handle all edge cases I guess.
For completeness, I should mention that the main
in my package.json
points to dist/main/main.js
as in production the app is transpiled into a dist
folder. This is the reason why, in development mode, I must run the app with electron main/main.js
instead of electron .
So it turned out that __filename
returns the location of electron-reload
and not the parent file require
ing it. So I was wrong. But I changed it to const mainFile = module.parent.filename
and I suppose it should work now. I'll publish a new version and you can try it then.
For the records, 1.4.1 works for my setting. Thanks!