Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS require without var

{This has nothing to do with Twitter}

Bit of an interesting question, interesting in that its probably stupid and you can laugh but I will have at least an answer to this damn itch.

Currently I use

var Bootstrap = require('library/Bootstrap');
Bootstrap.run();

When what would be really great is if I could do something like this in the Bootstrap index.js

module.exports.Bootstrap = My_Bootstrap;

And call it willy nilly like this

require('library/Bootstrap');
Bootstrap.run();

Without having to declare another variable to my space, is there a way to do this or am I staring at a screen wondering, dreaming, getting lost, coming back and wasting time?

edit So this is what I did in the end:

I created a single global object and am only adding important modules to it so they can be accessed and instantiated. This turned out to be an amazing answer and solution and really handy

like image 810
Dave Mackintosh Avatar asked Jul 16 '12 14:07

Dave Mackintosh


People also ask

Why is require () used in NodeJS?

In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

Why does NodeJS use require instead of import?

The major difference between require and import , is that require will automatically scan node_modules to find modules, but import , which comes from ES6, won't. Most people use babel to compile import and export , which makes import act the same as require . The future version of Node.

Should I use import or require in NodeJS?

Using require to load an ES module is not supported because ES modules have asynchronous execution. Instead, use import() to load an ES module from a CommonJS module.

Does NodeJS require JavaScript?

Node. js follows the CommonJS module system, and the built-in require function is the easiest way to include modules that exist in separate files. The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the exports object.


2 Answers

Nope, you have to declare the variable. You could get inside the require function, though, and make that do the work for you. I'm not overly familiar with Bootstrap's internal architecture but it seems like something like this should do the trick:

// put this in some out-of-the-way cubbyhole somewhere
var oldRequire = require;
function require(path) {
    Bootstrap = oldRequire(path);
}

// and this would go in your main file
require("library/Bootstrap");
Bootstrap.run();
like image 27
Elliot Bonneville Avatar answered Oct 09 '22 03:10

Elliot Bonneville


Well you can just modify the global object. This is similar to window object on browsers. So you can write a wrapper for your module like the following:

global.Bootstrap = My_Bootstrap

The global object is shared between modules. Then you can just use with Bootstrap wherever you are.

like image 190
Farid Nouri Neshat Avatar answered Oct 09 '22 02:10

Farid Nouri Neshat