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

Setting base directory to resolve relative requires #2414

Closed
mattdesl opened this issue Aug 4, 2015 · 7 comments
Closed

Setting base directory to resolve relative requires #2414

mattdesl opened this issue Aug 4, 2015 · 7 comments

Comments

@mattdesl
Copy link
Contributor

mattdesl commented Aug 4, 2015

This is similar to #1971, but I'm looking to fix relative requires as well.

Using the following with node-integration, everything is working fine:

window.loadUrl('file://' + __dirname + '/index.html')

However, when index.html and the client index.js is served on localhost, the require paths are not getting resolved correctly. Is there a way to set the path for relative requires?

require('./relative/path/foo.js') <-- cannot find module

The issue can be reproduced here:
https://github.com/mattdesl/test-electron-require

My main reason for using localhost is for a smoother development experience: I can stream transpiled source (i.e. webpack, browserify, babel) to the response, rather than wiring up a build step with static files.

@mattdesl
Copy link
Contributor Author

mattdesl commented Aug 4, 2015

Update: Using a local host instead of file:// also seems to change __filename and __dirname semantics.

@zcbenz
Copy link
Member

zcbenz commented Aug 5, 2015

require can only read a file on file system, if you load the page with any protocol other than file://, there is no way to make require read relative path unless you set NODE_PATH yourself.

@zcbenz zcbenz closed this as completed Aug 5, 2015
@mattdesl
Copy link
Contributor Author

mattdesl commented Aug 5, 2015

I have been testing by changing NODE_PATH, which fixes require statements like require('foo'). However, the base path for relative requires, as well as __dirname and __filename behave a bit differently.

I guess there is no way of resolving this without a source transform like browserify?

@zcbenz
Copy link
Member

zcbenz commented Aug 5, 2015

I guess there is no way of resolving this without a source transform like browserify?

No, this is a limitation of Node.js, the whole module system only works on filesystem.

@kpdecker
Copy link

require does only operate on the filesystem, but it doesn't seem out of line to allow for the paths to be manually set so you can lookup within the file system of the local project when loading from a HTTP endpoint. This is effectively the same concept as the ability to lookup from the electron base libraries that is currently done when require is called from a HTTP loaded module, as the relative paths are "~/electron-prebuilt/dist/Electron.app/Contents/Resources/atom.asar/renderer/lib/init.js" even though this is HTTP.

@mjangir
Copy link

mjangir commented Sep 6, 2018

After debugging for one week, I found this and it quickly resolved my issue. In my dev mode, I was directly loading localhost in electron main process window and was trying to reference some externally exposed webpack dependencies that was throwing error of cannot find module.

Though I was using html-webpack-plugin that creates a hidden html file from template in memory and injects all webpack bundles into it. So I had no idea how to load that in memory html file with file:// protocol in electron main window. Then I found html-webpack-harddisk-plugin that fixed everything.

Thank you everyone for giving me the hint.

@cawoodm
Copy link

cawoodm commented Mar 31, 2019

I'm having the same problem: My index.html (in root /) references ./js/main.js (in a script tag) which does require("./pow"). The error is "Cannot find module './pow'" and I can see it's looking in root / relative to index.html and not in /js which is where the file pow.js is. Is there any way to fix/workaround this? Surely require should work relative to the path of the .js that called it??

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

5 participants