{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
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.
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.
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.
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With