Add global function to every execution of NodeJS

Leonardo Rossi

In my project folder project I have a file utils.js like this

global.root_path = process.cwd();
global.custom_require = function() {
    console.log('custom require called');
}

I would like to include this file in every execution of node so that in every js file in project i can call custom_require, or access global.root_path as I'm doing already with built-in function require.

Do you know if there is a solution (command line options, environment variable or whatever) to achieve this?

UPDATE I don't want to include utils.js in every file, in that case I wouldn't need to modify global object.

Thanks!

bevacqua

Simply require it. Example app.js:

require('./utils.js');

var express = custom_require('express');
var app = express();
var port = process.env.PORT || 3000;

app.listen(port);

Then, you could just do (in terminal)

node app

As long as you require the code containing the global declarations on it, then you can use those globals from any file that's required after the fact.

You most probably don't want to have a custom "plugin" altering the behavior of all your applications when run locally, thus it'd be best to stay away from that kind of pattern. Instead, you might want to create a module, use npm link on it, and then npm link something in your project's directory, where something is the module name.

Then each project could just add one line like require('something'); at the very beginning. Changing something would immediately impact all the projects which included it, thanks to the behavior in npm link.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to chain execution of array of functions when every function returns deferred.promise?

Record every method execution

order of code execution in async await not working properly in a function in nodejs?

Function execution context, browser vs. nodejs

Execution of initstate for every instance

Is there a function or a method to add " , " after every row in VSC?

How do function handle global objects in python? What is the execution order?

How to measure the execution time of an asynchronous function in nodejs?

How to add standard routine to every function in array of function pointers?

Add global error in validate function

Function Calling Execution in nodejs

Ember.js run function on route change for every single route. Global activate function?

How to add 2 list from a function without using global variables?

Does nodejs global variable bind to "module" function, how to assign?

Add function to external module in nodejs

Trying to write my first "global" function for my nodejs controllers

Foreach not working pushing every record to promise function in NodeJS

How to ensure that a custom function runs after execution of every Python Unittest?

JavaScript variable without var inside function not available in Global Execution Context

firebase Nodejs async cloud function not finishing execution

Avoiding execution of function many times - process.env or global variables

How to make global function in nodejs expressjs?

A function returns different values in every execution

Is there a way to list all nodejs function execution time?

How to add time delay for every 10 lists of completion in python threadpool execution?

Call a function every X second in a loop in NodeJS

VS code add parameter to a function and add it to every call of the function

Nodejs Set a global function and call a nested function

How to wait for a function execution in NodeJS