📜 ⬆️ ⬇️

Export, import to Node.JS

It seems, everything is simple and clear:

var module = require("./some_module.js"); module.f(); //     console.log(module.obj); //     module(); //    

To export something in the module itself, you need to register:

 exports.f = function() { return 123; }; exports.obj = { name: "Foobar", age: 33 }; //   ,      //     ! module.exports = function() { return "I have not a name"; } 

Why not always work?
')
The answer to this question lies in understanding the work of the require () function.

When calling require (), as I understood, it creates an object called exports, reads the transferred file into a variable, and executes all the code from that variable. After that, it returns us the exports object that was modified during the execution of the code.

Let's try to create our own require () function. To simplify the perception, we will pass to our function not the file name, but already, as it were, the contents of the file as a string variable. Let's call her script.

The definition of our require function will look like this:

 function require(script) { var exports = {}; eval(script); return exports; } 

But what if we need to export an unnamed object? Remember module.exports = function() {} ? Add an object with the name module, and assign its object to its exports property.

Our require function is a bit more complicated:

 function require(script) { var module = {}; var exports = {}; eval(script); return module.exports ? module.exports : exports; } 

Run the test:

 var script = "exports.foo = 'foo'; exports.bar = 'bar';"; //   var foobar = require(script); //  console.log(foobar.foo); // : foo console.log(foobar.bar); // : bar 

Works!

We start the test with the export of an unnamed object:

 var script = "module.exports = function() { return 'foobar'; }"; //   var foobar = require(script); //  console.log(foobar()); // : foobar 

It works too.

We start the test with the export of anonymous object and named objects of the module:

 var script = "exports.arr = [1, 2, 3]; module.exports = function() { return 'foobar'; }"; //   var foobar = require(script); //  console.log(foobar()); // : foobar console.log(foobar.arr); // : undefined 

Does not work!

By the way, it does not work when using the standard node.js require () function. This shows that our function is not much different from it, and we are on the right track.

But how can we export both untitled and named objects? The answer is that you only need to use module.exports, moreover, named objects are necessarily exported after unnamed.

We will correct the previous example:

 var script = "module.exports = function() { return 'foobar'; }\n" + "module.exports.arr = [1, 2, 3];\n" + "module.exports.result = 'Ok'\n"; var foobar = require(script); //  console.log(foobar()); // : foobar console.log(foobar.arr); // : [1, 2, 3] console.log(foobar.result); // : Ok 

Works!

I hope that my examples, dear reader, helped you to understand more deeply the mechanism of export-import of modules to node.js. At first glance, it is very simple, as the developers themselves write about. But without an understanding of how the built-in function requires (), sometimes you find yourself alone with the wonderful results of your code.

Source: https://habr.com/ru/post/276791/


All Articles