我有以下代码片段,它在其上下文中工作.
"use strict";
require('chromedriver');
var selenium = require('selenium-webdriver');
var driver = new selenium.Builder()
.forbrowser('chrome')
.build();
我不明白的是这条线:
要求( ‘chromedriver’);
如果我删除它我得到一个错误:
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函数.
所以关于这条线的问题:require(‘chromedriver’);
它为什么有效?
所需的chromedriver最终在哪里?
如果require()函数没有将其返回保存到变量中,那么在genereal中会发生什么?
解决方法
在模块上调用require实际上执行模块中的任何代码.在大多数情况下,模块会导出一个或多个函数或一个对象,您希望将其存储在变量中.但如果你写的东西如下:
for (var i = 0;i < 100; i++){
console.log("I've been called %d times",i);
}
在.js文件中,然后在节点程序中需要该文件,您将获得100行添加到您的控制台,没有其他任何事情发生.