Could someone explain how require() and console.log work in javascript? I have a code snipped saying something close to this one:
var somevar = require('C:/Users/Me/Folder/SomeFolder/somefile.js');
console.log(somevar);
In the console I see the following:
{ _maxListeners: 0,
reset: [Function],
verify: [Function],
report: [Function],
getSource: [Function],
getSourceLines: [Function],
getAllComments: [Function],
getComments: [Function],
getJSDocComment: [Function],
getAncestors: [Function],
getNodeByRangeIndex: [Function],
getScope: [Function],
markVariableAsUsed: [Function],
getFilename: [Function],
defineRule: [Function],
defineRules: [Function],
defaults: [Function] }
somefile.js here is a huge file with javascript code (more then 32 000 lines). To be precise it's ESLint in my case.
Why does it print only these results in console? There are many more functions in somefile.js then listed above and I expected to see more.
And after simply requiring eslint module from Node.js var eslintfile = require('eslint');
the output in console is slightly different:
{ linter:
{ _maxListeners: 0,
reset: [Function],
verify: [Function],
report: [Function],
getSource: [Function],
getSourceLines: [Function],
getAllComments: [Function],
getComments: [Function],
getJSDocComment: [Function],
getAncestors: [Function],
getNodeByRangeIndex: [Function],
getScope: [Function],
markVariableAsUsed: [Function],
getFilename: [Function],
defineRule: [Function],
defineRules: [Function],
defaults: [Function] },
cli: { execute: [Function] },
CLIEngine: [Function: CLIEngine] }
Why is it so?
There is no purpose whatsoever in the code above. I was just playing with it. I only need an explanation. Any suggestions, links and thoughts are much appreciated.
And what should I do to see all functions in the code and find one I need to print it then in console or write in a different file?
The require
function will only retrieve whatever the module is exporting (through the module.exports
object, which is typical in a CommonJS module). Since the general behavior of console.log
on objects is to inspect them up to a certain depth, the explanation that you seem to be requesting is that the two modules are actually different and exporting a slightly different range of attributes. Although both are the same version of ESLint, the first file is the bundled (browserify'd in this case) ESLint API, which does not contain the CLI engine. This can be found out by observing the package.json and Makefile.js in the ESLint repository.
When require
ing the library by its package name in Node, the "main" script is loaded, which according to package.json, is "./lib/api.js". This script will load both the linter and a few extra objects that relate to the command line interface.
By looking at "Makefile.js" in line 44, you can see that the building operation with browserify will instead bundle "eslint.js" and its dependencies, without including anything else (the CLI part, that is). This bundling is also triggered by the test
command, which was invoked by the "test" script in the package.
In short, the two modules are different, even though consisting of the same package. This leads to a different output by console.log
.