Stephan Ahlf
Stephan Ahlf

Reputation: 3497

How to change the Node.js module wrapper?

For testing purposes I need to change the Node.js Module wrapper.

(function (exports, require, module, __filename, __dirname, process, global) {  
    debugger;
 });

Played around with Module I found

var Module = require("module")
Module.wrapper
-> ["(function (exports, require, module, __filename, __dirname, process, global) { ", "
});"]

Module.wrap
-> function(script) {
    return NativeModule.wrapper[0] + script + NativeModule.wrapper[1];
}

Is it possible to hook into Module.wraper or property to change the script wrapping?

Upvotes: 17

Views: 5803

Answers (3)

Michał Perłakowski
Michał Perłakowski

Reputation: 92461

You've done a great job by finding the Module.wrap function. How about simply overwriting it?

Imagine that you have two files, main.js and module.js. In main.js you overwrite the Module.wrap function in order to console.log('debug'); every time a module is required. Then you require module.js, which contains a hello world message.

main.js:

var Module = require("module");

(function(moduleWrapCopy) {
  Module.wrap = function(script) {
    script = "console.log('debug');" + script
    return moduleWrapCopy(script); // Call original wrapper function
  };
}(Module.wrap)); // Pass original function to IIFE

require("./module.js");

module.js:

console.log("Hello world from module.js!");

Executing node main.js results in:

debug
Hello world from module.js!

This also works with nested require() calls, for example if you require("./module2.js") in module.js:

module.js:

console.log("Hello world from module.js!");

require("./module2.js");

module2.js:

console.log("Hello world from module2.js!");

In this case node main.js produces:

debug
Hello world from module.js!
debug
Hello world from module2.js!

Tested on Node.js 4.3.0 and 6.1.0.

Upvotes: 16

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24590

You can override, the require by your own method. For example

require = function(module_name) {
  var js = openTheModuleIndexFile(module_name)
  var wrapped = wrapTheModule(js)
  eval(wrapped)
}

All you have to do is to implement the openTheModuleIndexFile function, and wrapTheModule which your wrapping function.

(Don't forget the module_name can be exists is node_modules of this directory, parent directory, or child directery, it's even can be just a JS file name)

I'm sure, you need to make same hacks, but in the end, it should work.

Upvotes: 3

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24590

You cannot change the wrapper inside node app, without override the require function. but you can fork (copy) node source to your computer, change one line & compile (build) it.

This why he love open-source :)

All you have to do is change one line of code if src/node.js file

here: https://github.com/nodejs/node/blob/master/src/node.js#L990

NativeModule.wrapper = [
    '(function (exports, require, module, __filename, __dirname) { ',
    '\n});'
  ];

Upvotes: 0

Related Questions