Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js require without storing it into a variable

Tags:

I have the following code snippet and it works in its context.

"use strict"; 
require('chromedriver');
var selenium = require('selenium-webdriver');
var driver = new selenium.Builder()
  .forBrowser('chrome')
  .build();

What I don't understand is the line:

require('chromedriver');

If i remove it I get an error:

Error: The ChromeDriver could not be found on the current PATH. Please download the latest version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and ensure it can be found on your PATH.

So it does something.

I understand what var chromedriver = require('chromedriver'); does and I have only seen the require function being used that way so far.

So my questions regarding the line: require('chromedriver');

Why does it work?

Where does the required chromedriver end up?

What happens in genereal if the require() function does not save its return into a variable?

like image 237
tool Avatar asked Apr 05 '16 12:04

tool


People also ask

What can I use instead of require in NodeJS?

Use Import Instead of Require in Node App.

For what require () is used in NodeJS?

In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

Should I use import or require in NodeJS?

mjs' extension. NOTE: You must note that you can't use require and import at the same time in your node program and it is more preferred to use require instead of import as you are required to use the experimental module flag feature to run import program.

What happens when we require a module in NodeJS?

If the module is native, it calls the NativeModule. require() with the filename and returns the result. Otherwise, it creates a new module for the file and saves it to the cache. Then it loads the file contents before returning its exports object.


2 Answers

The basic thing require does is that it executes code written in the module. At the end this executed code might or might not return something. In your case it doesn't matter what this code returns, rather what matters is that this code is executed at least once.

It is also important to note that the result of require is cached. This means even if you require that module multiple times the "code" would execute only once.

This whole paradigm of modules and require comes from CommonJS pattern, I would suggest you to read it.

like image 138
Haris Hasan Avatar answered Oct 20 '22 18:10

Haris Hasan


Calling the require on the module actually executes whatever code is in the module. In most cases, the module exports one or more functions or an object, which you want to store in a variable. But if you were to write something like:

for (var i = 0;i < 100; i++){
   console.log("I've been called %d times", i);
}

in a .js file and then require that file in a node program, you'd get 100 lines added to your console and nothing else happening.

like image 36
Paul Avatar answered Oct 20 '22 18:10

Paul