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

Relative path resolution for require does not work #2

Open
kahlil opened this issue Jan 16, 2018 · 8 comments
Open

Relative path resolution for require does not work #2

kahlil opened this issue Jan 16, 2018 · 8 comments
Labels
💵 Funded on Issuehunt This issue has been funded on Issuehunt enhancement New feature or request help wanted Extra attention is needed

Comments

@kahlil
Copy link

kahlil commented Jan 16, 2018

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.

@kahlil
Copy link
Author

kahlil commented Jan 16, 2018

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.

@juliangruber
Copy link
Contributor

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

@Frixoe
Copy link

Frixoe commented Nov 6, 2018

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

@sindresorhus sindresorhus added enhancement New feature or request help wanted Extra attention is needed labels Mar 15, 2019
@IssueHuntBot
Copy link

@IssueHunt has funded $40.00 to this issue.


@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label May 10, 2019
@turkerdev
Copy link

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

@eduardormw
Copy link

eduardormw commented Aug 7, 2019

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.

@lucasvog
Copy link

lucasvog commented Mar 30, 2020

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.

@bharring
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💵 Funded on Issuehunt This issue has been funded on Issuehunt enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

9 participants