electron-serve
electron-serve copied to clipboard
Relative path resolution for require does not work
When I use electron-serve in electron-quick-start require('./renderer.js')
can't be resolved.
I have to use path.resolve
within the require
call to make that work:
<script>
const path = require('path');
require(path.resolve('./renderer.js'));
</script>
Any way that can be fixed so that relative require
s work with electron-serve?
There is a $40.00 open bounty on this issue. Add more on Issuehunt.
- Checkout the Issuehunt explorer to discover more funded issues.
- Need some help from other developers? Add your repositories on Issuehunt to raise funds.
Btw.: if I require the renderer.js file via script:src tag then I have to use path.resolve
in renderer.js if I want to require something with a relative path.
works for me: use <script src="/renderer.js"></script>
, then let webpack handle any relative requires
@juliangruber he wasn't talking about using the script
tag.
Relative paths don't work when requiring a module which is not being served from a js file which is.
I am currently facing the same problem. It would be great if someone could come up with a solution which doesn't involve resolving the path of the external module as I don't think that would work in production.
@issuehunt has funded $40.00 to this issue.
- Submit pull request via IssueHunt to receive this reward.
- Want to contribute? Chip in to this issue via IssueHunt.
- Checkout the IssueHunt Issue Explorer to see more funded issues.
- Need help from developers? Add your repository on IssueHunt to raise funds.
Please help. This require thing doesnt work for me too.
Hi y'all, doesn't this fix your issue?
https://stackoverflow.com/a/55908510/9034699
I tested with electron-quick-start and I was able to get require('./renderer.js')
resolved.
I may not have the solution, but some information for people who have the problem of the require('./localfile')
not working in their script.js
which they embedded in their HTML-file by using <script src='./path/to/script.js'>
.
The problem is, that relative require can apparently only be used in files that are required themselves.
So to fix that, you can actually use
<script>require('./path/to/script.js');</script>
to import the file, instead of <script src='....
.
that might fix the issue.
If typescript is used and require()
is still not working, try using import x from './localfile'
instead after importing the file in the way shown above.
Thanks for this great project, this has been very helpful!
I've run into a similar issue with path resolution when calling loadURL()
. Please let me know if this is the best place to report it. Essentially I have two browser windows serving two different apps, from two different subdirectories , e.g. ~/www/app1
and ~/www/app2
(after setting { directory: '~/www' }
). Trying to call loadUrl
with app://-/app1
and app://-/app2
does not work, neither does app://app1
. Both report this error UnhandledPromiseRejectionWarning: Error: ERR_FILE_NOT_FOUND (-6) loading 'app://-/'
Is this supported, or do you know of a workaround?