Node.js 需要而不将其存储到变量中

Node.js require without storing it into a variable(Node.js 需要而不将其存储到变量中)
本文介绍了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');

为什么会起作用?

所需的 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 需要而不将其存储到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)