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

Globals don't propagate to required modules #11

Closed
domenic opened this issue Jul 31, 2012 · 4 comments
Closed

Globals don't propagate to required modules #11

domenic opened this issue Jul 31, 2012 · 4 comments

Comments

@domenic
Copy link
Collaborator

domenic commented Jul 31, 2012

I was trying very hard to do

var domify = sandboxedModule.require("domify", { globals: { document: myFakeDocument } });

But domify resolves to /node_modules/domify/index.js, which is just

module.exports = require('./lib/domify');

But if I insert a console.log(document) at the top of /node_modules/domify/lib/domify.js, it fails, indicating the document global didn't make it through the dependency chain.

domenic added a commit that referenced this issue Feb 12, 2013
@jamesdavidson
Copy link

Any ideas on how to do this @felixge or @domenic? It would be really handy!

@domenic
Copy link
Collaborator Author

domenic commented Feb 20, 2013

I gave it a shot, but it's pretty tricky. You basically need to inject some kind of sandboxed module require into the sub-dependencies, but that's not easy to do with the current codebase.

You can work around it like so:

var domify = sandboxedModule.require("domify", {
  globals: { document: myFakeDocument }
  requires: {
    "./lib/domify": sandboxedModule.require("domify/lib/domify", {
      globals: { document: myFakeDocument }
    })
  }
});

(it might be "domify/lib/domify" instead of "./lib/domify", not sure.)

@jamesdavidson
Copy link

Ah, the exact same workaround I'm currently hacking on. It'll do for now. Thanks mate

@thlorenz
Copy link

thlorenz commented Jun 1, 2014

We recently added this feature to proxyquire.

I'm not sure if that helps you at all since it works with require extensions (so may not apply here), but I thought it might help you to have a look.

If you prefer to not read diffs, it all happens in here.

ddimitrioglo pushed a commit to MitocGroup/node-sandboxed-module that referenced this issue Feb 21, 2018
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

3 participants