Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify NodeJS core programs

I tried to add some custom functions into the FS module of NodeJS, this module is part of NodeJS' core programs. I found the corresponding file (fs.js) in the following location: /usr/lib/nodejs. The problem was that the changes that I've made seemed to not affect anything when the corresponding module and function was called.

What I did was I added a function like this in /usr/lib/nodejs/fs.js:

      fs.someRandomFunc = function(){return 'Yeah!'}

However, when I called the function, it replied :

      var fs = require('fs')
      console.log(fs.someRandomFunc())

      // Error Message
      TypeError: Object #<Object> has no method 'someRandomFunc'

By the way, this also happens to the other core modules, such as module.js and path.js. Does this happen because NodeJS caches the core JS program instead of loading it from /usr/lib/nodejs?

Any idea to resolve this issue would be appreciated.

Thanks!

like image 961
ebudi Avatar asked Aug 23 '26 05:08

ebudi


1 Answers

fs is part of NodeJS' Core Modules. As such, it is compiled into binary and distributed. So modifying the source is not going to take effect unless you recompile them.

It is anyway not a good idea to modify Node's source files directly. What you can/should instead is extend the existing fs module with your own functions, like graceful-fs does, or replace it entirely with your version, like fs-extra does.

like image 65
Mrchief Avatar answered Aug 24 '26 17:08

Mrchief