0

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!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

That's what I want to avoid, I edited the question. Requiring that file means that I need to know where it is located, relatively to the file I'm working on. I would like to have those global data as it's built in in node (Avoiding modify and recompile nodeJS from scratch :) )
If you make it into a module and use npm link as I described, you wouldn't need to know the relative path to that file, you could just require it using the module name like any other package. See npm link.
Sorry I didn't read carefully your answer. I played a little with npm link as you suggests and discovered that it's what I'm looking for. Do you know if there is a way to to a npm link on every subfolder of my project? (I mean , without a script :)) Thanks dude!
why would you need to do it on every folder? just give a package.json to the module you're going to include for the global type stuff, and then you can npm link on the directory containing that. Then, in any given project, do npm link {name}, where {name} would be the name you gave to the module in its package.json
This code must be loaded on many machines and I cannot manually do a npm link, that's why I need an automatic way to link globally all the modules I write.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.