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!
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 require
d 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.
Comments