admin管理员组

文章数量:1244304

Friend has asked an interesting question and I've tried a few things but to no avail, is there any way to override a Node JS module?

For instance, I want to override the readFile function to use an S3 bucket instead of the filesystem. I.E:

var fs = require('fs');

fs.readFile('my_text_file.txt', ...);

Actually runs something like this

FileSystem.readFile = function () {
    // Connect to S3 and retrieve remote file
}

I've tried the prototype but it seems they've set up native modules without the __proto__ object, they don't have a .constructor property that means anything to anyone.

I've thought about using Nodes VM but this is too strict as I want the user to be able to install modules via npm and use them.

The closest I've actually e is creating a new module (since I can't put a file named fs.js in my node_modules folder and require it; it just gets ignored) and just hard-setting the values of fs to what I want but this isn't quite right, I want the user to be using require('fs') and use my custom function.

Is this at all possible without piling my own version of Node JS?

Friend has asked an interesting question and I've tried a few things but to no avail, is there any way to override a Node JS module?

For instance, I want to override the readFile function to use an S3 bucket instead of the filesystem. I.E:

var fs = require('fs');

fs.readFile('my_text_file.txt', ...);

Actually runs something like this

FileSystem.readFile = function () {
    // Connect to S3 and retrieve remote file
}

I've tried the prototype but it seems they've set up native modules without the __proto__ object, they don't have a .constructor property that means anything to anyone.

I've thought about using Nodes VM but this is too strict as I want the user to be able to install modules via npm and use them.

The closest I've actually e is creating a new module (since I can't put a file named fs.js in my node_modules folder and require it; it just gets ignored) and just hard-setting the values of fs to what I want but this isn't quite right, I want the user to be using require('fs') and use my custom function.

Is this at all possible without piling my own version of Node JS?

Share Improve this question asked Jun 12, 2013 at 19:58 Dave MackintoshDave Mackintosh 2,7962 gold badges32 silver badges41 bronze badges 4
  • I haven't worked with node.js so this might not work at all. You could try to see what function declares FileSystem and then use that as a prototype for your custom object ("subclass" it). MyFS={FileSystem.call(this);};MyFs.prototype=new FileSystem(); Or use something like goog.base and goog.inherit to create a subclass that can call it's "parent" functions and deals with parameters passed to the constructor: docs.closure-library.googlecode./git/… Note that badse uses arguments.callee.caller wich doesn't work in ecma 5 strict so you have to re write that. – HMR Commented Jun 13, 2013 at 0:41
  • Good article about goog.base and goog.inherits is here: bolinfest./essays/googbase.html – HMR Commented Jun 13, 2013 at 0:42
  • I made FileSystem up for illustrative purposes, the modules don't have a __proto__ object and thus no constructor. The closest I've gotten is to directly set properties on the object and including another script but I want to require('fs') and it have my custom functionality – Dave Mackintosh Commented Jun 13, 2013 at 9:35
  • Yea, I just checked out fs.js and doesn't look like node.js uses constructor functions or at least I can't find it. – HMR Commented Jun 13, 2013 at 10:03
Add a ment  | 

1 Answer 1

Reset to default 13

I feel obliged to strongly warn you against overriding native functions. That being said, this will work:

main.js:

var fs = require('fs'),
    oldReadFile = fs.readFile;

fs.readFile = function (filename, options, callback) {
  console.log('hey!');
  oldReadFile(filename, options, callback)
};

var other = require('./other');
other.test();

other.js:

var fs = require('fs');

exports.test = function () {
  fs.readFile('./main.js', {encoding: 'utf8'}, function (err, data) {
    console.log(err, data);
  });
};

You'll need to wrap your user's script with your own to first override what you want.

本文标签: javascriptNode JS override standard moduleStack Overflow