Node.js要求不将其存储到变量中 [英] Node.js require without storing it into a variable

查看:128
本文介绍了Node.js要求不将其存储到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码段,并且可以在其上下文中使用.

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();

我不明白的是那行:

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.

所以它有作用.

我了解var chromedriver = require('chromedriver');的功能,并且到目前为止,我只看到了require函数正在使用的方式.

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

关于这条线的我的问题是:require('chromedriver');

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

为什么起作用?

所需的chromedriver在哪里结束?

Where does the required chromedriver end up?

如果require()函数未将其返回值保存到变量中,那么在一般情况下会发生什么?

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

推荐答案

在模块上调用require实际上会执行模块中的任何代码.在大多数情况下,模块导出要存储在变量中的一个或多个函数或对象.但是,如果您要编写类似这样的内容:

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);
}

在.js文件中,然后在节点程序中require该文件中,您将向控制台添加100行,并且什么也没有发生.

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.

这篇关于Node.js要求不将其存储到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆