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

Uncaught Exception: Cannot find module ../package.json #58

Closed
marcoancona opened this issue Jul 10, 2019 · 8 comments
Closed

Uncaught Exception: Cannot find module ../package.json #58

marcoancona opened this issue Jul 10, 2019 · 8 comments

Comments

@marcoancona
Copy link

marcoancona commented Jul 10, 2019

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()

@mscoobby
Copy link

You can use something like path.join(__dirname, '..') to access files from the location of you entry point.

@marcoancona
Copy link
Author

marcoancona commented Jul 10, 2019

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

const config = require(path.join(appPath, 'package.json'))

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.

@marcoancona marcoancona changed the title Cannot find module ../package.json Uncaught Exception: Cannot find module ../package.json Jul 10, 2019
@mscoobby
Copy link

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

@yan-foto
Copy link
Owner

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.

@yan-foto
Copy link
Owner

I think we can simplify the application logic by removing

const config = require(path.join(appPath, 'package.json'))
and replacing
const mainFile = path.join(appPath, config.main || 'index.js')
with

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.

@marcoancona
Copy link
Author

marcoancona commented Jul 11, 2019

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 .

@yan-foto
Copy link
Owner

So it turned out that __filename returns the location of electron-reload and not the parent file requireing 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.

@marcoancona
Copy link
Author

For the records, 1.4.1 works for my setting. Thanks!

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

No branches or pull requests

3 participants