dalejung / inode

Interactive Node.js
MIT License
3 stars 1 forks source link

require('./relative.js') not working within %run #1

Closed dalejung closed 11 years ago

dalejung commented 11 years ago

relative requires work within the inode_client but anything run through %run does not work.

dalejung commented 11 years ago
var id = resolvedModule[0];
var paths = resolvedModule[1];
var filename = Module._findPath(request, paths);

running that in the relative.js works fine... hm. It looks like the '.' path is not being resolved right?

dalejung commented 11 years ago
62 // hack for require.resolve("./relative") to work properly.
 63 module.filename = path.resolve('repl');

Found this. I'm assuming that module.filename is being used to determine what . is within a module? I mean, that much is obvious. Not sure why it would need to be explicitly set for repl though.

dalejung commented 11 years ago

ah, so repl uses that line to set its path to wherever the REPL was started. It passes in its require to context, so it needs to make sure its own require is changed to work as if repl.js itself was in the REPL exec path.

dalejung commented 11 years ago
  sandbox.require = require;
  sandbox.exports = module.exports;
  sandbox.__filename = filename;
  sandbox.__dirname = dirname;
  sandbox.module = module;
  sandbox.global_ns = sandbox;

was the issue. I took the lines from module.js without really understanding why they were there. Normally the process for require is to create a Module object and then run its code. Passing in the exports or module doesn't make sense here since we're not creating a module, we're just running straight code.

Plus, the module being passed in would be the inode.js, which makes no sense.

The real issue though was sandbox.require = require;, we would be passing in the inode's require which wouldn't be pathed to the correct spot. We could do the same repl pathing hack, but it's easier to just delete the line and let the repl.require take precedence, since that's already fixed.

Looking further, it's possible to create a Module and have the import path be set to the %run files path. Currently it will be locked to where the inode instance was startup.