Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - Standard Library

Is there a way in Node.js to define a "Standard Library", which is automatically executed at the start of every Node.js App?

For example:

//stdlibrary.js
function sayHello(){
   console.log("Hello World!");
}

//app.js
sayHello(); //without including the stdlibrary.js!

Hope my question is clear.

Thanks for your help!

Update

I´m looking for something like the auto_prepend_file in PHP. Hope there is something similar to this.

like image 569
Van Coding Avatar asked Apr 26 '26 17:04

Van Coding


2 Answers

Yes, this is simple to do. Just edit the src/node.cc file to include an option for an autorequire file, and then modify the node::Load code to handle the option. You probably also need to change the javascript in src/node.js.

Rebuild, test and you are done.

Or you could probably just hack this into src/node.js by having it eval a string that requires your library and then evals the actual script file mentioned on the command line.

like image 134
Michael Dillon Avatar answered Apr 29 '26 07:04

Michael Dillon


There's no such thing in node.js, this kind of magic is not transparent and because of this reason gladly avoided by the most software products.

like image 32
Tobias P. Avatar answered Apr 29 '26 05:04

Tobias P.