Browserify: Missing require.main

Created on 22 Feb 2015  路  3Comments  路  Source: browserify/browserify

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.

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.require thing myself, or at least get a string to the root of the project from anywhere?

All 3 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrepadez picture andrepadez  路  7Comments

timwis picture timwis  路  4Comments

thlorenz picture thlorenz  路  5Comments

ghost picture ghost  路  4Comments

3rd-Eden picture 3rd-Eden  路  10Comments