Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all global variables in Node.js

I'm trying to list all of the global variables, including those refering to built-in objects.

In Chrome's console I can simply type this and get back all the keys, including things like String, Number, etc.

However when I do this in Node.js I get much less:

> Object.keys(this)
[ 'global',
  'process',
  'GLOBAL',
  'root',
  'Buffer',
  'setTimeout',
  'setInterval',
  'clearTimeout',
  'clearInterval',
  'setImmediate',
  'clearImmediate',
  'console',
  'module',
  'require',
  '_' ]
> this.eval
[Function: eval]

Where is this.eval coming from?

like image 840
ldmat Avatar asked Jul 01 '15 23:07

ldmat


People also ask

How do I see all global variables?

Use the env command. The env command returns a list of all global variables that have been defined. If a global variable exists in a script that hasn't been run yet, it will not show up in the output from env .

What are the global variables in NodeJS?

What are Global Variables? Global variables are variables that can be declared with a value, and which can be accessed anywhere in a program. The scope of global variables is not limited to the function scope or any particular JavaScript file. It can be declared at one place and then used in multiple places.

How do you check environment variables in NodeJS?

To retrieve environment variables in Node. JS you can use process. env. VARIABLE_NAME, but don't forget that assigning a property on process.

What is __ Dirname NodeJS?

It gives the current working directory of the Node. js process. __dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.


2 Answers

The built-in properties of the global object are non-enumerable, so Object.keys doesn't return them. You can use Object.getOwnPropertyNames instead.

like image 200
Bergi Avatar answered Sep 23 '22 21:09

Bergi


The following globals() function will get you global namespace object:

function globals() { return this; }

With it you can list all variables of global namespace anytime you want:

function varsList() {
  return Object.getOwnPropertyNames(globals());
}
like image 20
c-smile Avatar answered Sep 25 '22 21:09

c-smile