I'm looking to monkey-patch require() to replace its file loading with my own function. I imagine that internally require(module_id) does something like:

Convert module_id into a file path

Load the file path as a string

Compile the string into a module object and set up the various globals correctly

I'm looking to replace step 2 without reimplementing steps 1 + 3. Looking at the public API, there's require() which does 1 - 3, and require.resolve() which does 1. Is there a way to isolate step 2 from step 3?

I've looked at the source of require mocking tools such as mockery -- all they seem to be doing is replacing require() with a function that intercepts certain calls and returns a user-supplied object, and passes on other calls to the native require() function.

For context, I'm trying to write a function require_at_commit(module_id, git_commit_id), which loads a module and any of that module's requires as they were at the given commit.

I want this function because I want to be able to write certain functions that a) rely on various parts of my codebase, and b) are guaranteed to not change as I evolve my codebase. I want to "freeze" my code at various points in time, so thought this might be an easy way of avoiding having to package 20 copies of my codebase (an alternative would be to have "my_code_v1": "git://..." in my package.json, but I feel like that would be bloated and slow with 20 versions).

Update:

So the source code for module loading is here: https://github.com/joyent/node/blob/master/lib/module.js. Specifically, to do something like this you would need to reimplement Module._load, which is pretty straightforward. However, there's a bigger obstacle, which is that step 1, converting module_id into a file path, is actually harder than I thought, because resolveFilename needs to be able to call fs.exists() to know where to terminate its search... so I can't just substitute out individual files, I have to substitute entire directories, which means that it's probably easier just to export the entire git revision to a directory and point require() at that directory, as opposed to overriding require().

Update 2:

Ended up using a different approach altogether... see answer I added below

can you instead just include the node_modules folder in your git commits? That way when you checkout a commit you get a full snapshot of your entire project including the specific module versions. See here for more information futurealoof.com/posts/nodemodules-in-git.html
–
NoahJun 1 '13 at 4:53

Thanks... I'm trying to access earlier versions of my code FROM a later version of my code, though, so I don't think that will work. Also, when I started out with nodejs I went down the path of checking node_modules into git, and that seemed to wreak havoc with binary-compiled modules in a really bad way...
–
joshJun 1 '13 at 19:25

Basically, assuming your modules have a set of well-known extensions, you should be able to use this pattern of a function that takes the module and filename, does whatever loading/transforming you need, and then returns an object that is the module.

This may or may not be sufficient to do what you are asking, but honestly from your question it sounds like you are off in the weeds somewhere far from the rest of the programming world (don't take that harshly, it's just my initial reaction).

Thanks, I think module._compile may be what I'm looking for. I'll update the question if I get it working. Also, you could totally be right about me being in some weird corner -- any simpler way of doing what I'm trying to do (freeze successive old versions of my codebase and reference them from the latest version) would be helpful.
–
joshJun 1 '13 at 19:22