How can I list all the functions in my node js script?

I tried looking at global , but it only contains variables, not functions. How can I list all the functions created in my script?

+6
source share
5 answers

Run node debug from the command line with the file you want to look at. Then you can use a list (there are a lot of them)

 node debug mini_file_server.js < debugger listening on port 5858 connecting... ok debug> scripts 26: mini_file_server.js debug> list(1000) 1 var http = require('http'), 2 util = require('util'), 3 fs = require('fs'); 4 5 server = http.createServer(function(req, res){ 6 var stream = fs.createReadStream('one.html'), 7 stream2 = fs.createReadStream('two.html'); 8 console.log(stream); 9 console.log(stream2); 10 stream.on('end', function(){ 11 stream2.pipe(res, { end:false}); 12 }); 13 14 stream2.on('end', function(){ 15 res.end("Thats all!"); 16 }); 17 18 res.writeHead(200, {'Content-Type' : 'text/plain'}); 19 stream.pipe(res, { end:false}); 20 stream2.pipe(res, { end:true}); 21 22 }).listen(8001); 23 }); debug> 
+5
source

This is not possible in node without more advanced reflection tools such as a debugger.

The only way to do this is to use __parent__ , which has been removed due to security issues and more. As Mark Bessie said, when you run the script, these variables become module closing variables. You cannot access them elsewhere without exporting them explicitly.

This is not a mistake, it is by design. This is how node works. However, if you just ask your users to write a function expression assignment, everyone will work a-ok:

 module.exports = { a:function(){ //same logic you had in the function declaration } } 

Then you can easily flip and list modules.exports and get all the function names.

+3
source

If the function has a name, it will be displayed in global order:

 mb-work-laptop:~ markbessey$ node > for (var k in global) { console.log(k); } global process GLOBAL root Buffer setTimeout setInterval clearTimeout clearInterval console module require k > function z(a) { return a*10; } > for (var k in global) { console.log(k); } global process GLOBAL root Buffer setTimeout setInterval clearTimeout clearInterval console module require k z > > global.z [Function: z] 
+2
source

cli: http://nodejs.org/docs/v0.3.7/api/debugger.html

gui: https://github.com/dannycoates/node-inspector

There is also https://github.com/c4milo/node-webkit-agent in works that will be a more powerful version of the node inspector.

0
source

If you want to do AOP, the route will be AST.

You can create your own AOP structure with something like: http://esprima.org .

Or you could try node-burrito , great for not-so-complicated aspects:

 var burrito = require('burrito'); var src = burrito('someCall()', function (node) { if (node.name === 'call') node.wrap('qqq(%s)'); }); 

will generate

 qqq(somecall()) 
0
source

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


All Articles