Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js module how to get list of exported functions

Tags:

Given a node js module/package, is there some way I can extract out all functions exported by that module ?

For example - if the module has a js file with following code:

exports.tokenizer = tokenizer; exports.parse = parse; exports.slice = slice; exports.curry = curry; 

Then I would like to have a following listed as exports: tokenizer, parse, slice, curry

like image 468
Vibha Singhal Avatar asked Aug 27 '14 14:08

Vibha Singhal


People also ask

How node js modules are exported?

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.

What can you export with Module exports in node JS?

Module exports are the instruction that tells Node. js which bits of code (functions, objects, strings, etc.) to “export” from a given file so other files are allowed to access the exported code.

How do I export multiple functions?

To export multiple functions in JavaScript, use the export statement and export the functions as an object. Alternatively, you can use the export statement in front of the function definitions. This exports the function in question automatically and you do not need to use the export statement separately.


1 Answers

You can require the file and just map through the object keys, which would return an array of the names of the exported objects.

var myExports = require('./exported-file.js');  console.log(Object.keys(myExports)); 
like image 96
Ben Fortune Avatar answered Oct 16 '22 16:10

Ben Fortune