In node.js, the require.main property points to the root (or a "main) module of the process. If I run node app.js, require.main === module within the scope of the app.js file.
Why do I want this? I like to require() my main module for running integration and acceptance tests and I distinguish between a regular run and a test run based on the require.main module.
var myAppsMain = function () { ... };
if (require.main === module) {
myAppsMain();
} else {
exports.main = myAppsMain;
}
This makes the myAppsMain function be called when the module is run directly and exported if require()'d.
I think it's a better idea to just have separate files instead of loading up a file with different concerns like this. This would add heft to every bundle with very marginal utility, so i don't want to support it.
Understandable, thanks for the comment.
Would this be a good reason to add鈥昿erhaps optional鈥晄upport for require.main?
Is there a way I could add such a require.main.require thing myself, or at least get a string to the root of the project from anywhere?
Most helpful comment
Would this be a good reason to add鈥昿erhaps optional鈥晄upport for
require.main?Is there a way I could add such a
require.main.requirething myself, or at least get a string to the root of the project from anywhere?