electron-serve icon indicating copy to clipboard operation
electron-serve copied to clipboard

Relative path resolution for require does not work

Open khalillechelt opened this issue 7 years ago • 8 comments

Issuehunt badges

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 requires work with electron-serve?

There is a $40.00 open bounty on this issue. Add more on Issuehunt.

khalillechelt avatar Jan 16 '18 11:01 khalillechelt

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.

khalillechelt avatar Jan 16 '18 11:01 khalillechelt

works for me: use <script src="/renderer.js"></script>, then let webpack handle any relative requires

juliangruber avatar Apr 24 '18 20:04 juliangruber

@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.

Frixoe avatar Nov 06 '18 13:11 Frixoe

@issuehunt has funded $40.00 to this issue.


IssueHuntBot avatar Mar 21 '19 08:03 IssueHuntBot

Please help. This require thing doesnt work for me too.

turkerdev avatar May 10 '19 21:05 turkerdev

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.

eduardormw avatar Aug 07 '19 10:08 eduardormw

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.

lucasvog avatar Mar 30 '20 14:03 lucasvog

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://-/app2does 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?

bharring avatar Dec 29 '20 19:12 bharring