nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

'require' name conflict recommended workaround, does not work for external links

Open JolieOlsen opened this issue 8 years ago • 1 comments

nw.js Windows expose their own function called require(), when running in node context. Because of this, if you open a page in an nw.js Window which uses Node's built-in require(), the two will conflict.

This workaround has been supplied:

https://github.com/nwjs/nw.js/wiki/faq-name-conflict

Thank you for the workaround. I notice that this does work for pages which I open directly, via nw.Window.open. It works by adding the suggested javascript (which re-names nw.js' require() module) in to the <head> tag of the page being opened. However, there are situations where this page I'm opening, has links to other external websites, which also utilize Node.js' require(), but which I have no way to modify the code of. Since I do not have access to the code on those pages, and can not inject javascript in to them, this work around does not work in those situations.

Am I doing the work-around incorrectly? Is there some way to 'permanently' rename nw.js' require(), at the start of the nw.js process? So that it will take effect for any Window that gets opened during the life of the process?

(Note that I must be running in node context in my case, and my entrypoint is a javascript file, not an html file.)

Thank you for any suggestions.

======== TEMPLATE BUG FORM ========

NWJS Version : v0.34.1 (current latest) [nwjs-sdk-v0.34.1-osx-x64] Operating System : OSX (High Sierra)

Expected behavior

There should be some way to permanently re-name nw.js' require module, for the life of the nw.js process. (Or in general just some way to work around the issue of nw.js require conflicting with pages already utilizing require, such that I can use nw.js to open up any page, even those which I don't have access to the js/html of, and shouldn't hit name collisions for 'require'.)

Actual behavior

The workaround supplied seems to work only for pages you open directly in a new nw.js Window, which you have access to the javascript or html of. (Unless I am misunderstanding it, in which case, what is the proper way to utilize this work around?)

How to reproduce

You need to have two HTML pages, page A and page B, such that (1) page A and page B both use Node.js' require() in some included javascript, and (2) page B is accessed via an <a href tag in page A, which opens in to a new window (like <a href="pageB_url" target="_blank">). Open page A via nw.Window.open - notice the conflict for require() exists in page A, and also the new window for page B when you open it. Now open page A via nw.Window.open, but inject this js in to the <head> tag when doing so (the workaround code mentioned in https://github.com/nwjs/nw.js/wiki/faq-name-conflict):

<script type="text/javascript">
    window.requireNode = window.require;
    window.require = undefined; 
</script>

You'll see that page A's require() conflict is now resolved, but click on the link to page B, a new Window will be opened, and the conflict will remain there.

Suppose you have no access to page B code - this is the crux of the issue :( It makes sense that the code above doens't extend to page B - that's why I'm wondering if there's some other way to accomplish this in general, for all Windows that will be opened during nw.js process.

JolieOlsen avatar Oct 30 '18 19:10 JolieOlsen

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 26 '19 02:10 stale[bot]