Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS export function without object wrapper

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?

like image 664
700 Software Avatar asked May 03 '11 16:05

700 Software


People also ask

How do I export a node js function?

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.

Is module exports the same as export?

exports and module. exports are the same unless you reassign exports within your module.

How do I export a function from one node js file to another?

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.


1 Answers

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

like image 196
Raynos Avatar answered Sep 22 '22 16:09

Raynos