I am looking at Node.JS request and notice that you can use
var request = require('request'); request(...)
But when I try to do something similar like in the module I try
exports = function() {}
it does not work. The only way I know to use is
var request = require('request').request; request(...)
and
exports.request = function() {}
How can I set the whole export to a function instead of adding a function to the export object?
A hint might be available in the request source code but I am finding it hard to figure out what is going on. Can you help?
The module.js is used to export any literal, function or object as a module. It is used to include JavaScript file into node. js applications. The module is similar to variable that is used to represent the current module and exports is an object that is exposed as a module.
exports and module. exports are the same unless you reassign exports within your module.
To include functions defined in another file in Node. js, we need to import the module. we will use the require keyword at the top of the file. The result of require is then stored in a variable which is used to invoke the functions using the dot notation.
You need to overwrite it as
module.exports = function() {}
Merely writing exports = function() {}
creates a new local variables called exports and hides the exports
variable living in module.exports
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