I’ve been trying to add some convenience functions to Node’s file system module (mainly because it lacks some common sense things), but every time I begin fs.prototype.myfunc = in the repl, Node complains that I am trying to set a property of an undefined variable. Is it really true that you cannot access Node’s built-in module prototypes from the outside? If so, does anyone know a feasible workaround to extend Node’s built-in modules?
Just to note: I did require fs before trying to prototype it!
var fs = require('fs');
fs.prototype.myfunc = function() {}; //TypeError thrown here
What you get back in response to a require(”) depends upon the particular module. Some modules do this:
in that case, the value returned would be function and so would have a prototype you could attach things to.
Other modules just set values on the already existing exports.module object. E.g:
where module.exports is essentially just:
In the case of the fs module they do the latter:
So you get the error you do since the object returned isn’t a function. You’d get the same error if you did this:
Note you can just do: