语法错误:在严格模式下使用const?

42
我正在尝试使用selenium-webdriver登录facebook.com。
var webdriver = require('selenium-webdriver'),
    By = require('selenium-webdriver').By,
    until = require('selenium-webdriver').until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

driver.get('https://www.facebook.com/login');
driver.findElement(By.id('email')).sendKeys('****');
driver.findElement(By.id('pass')).sendKeys('*****');
driver.findElement(By.id('loginbutton')).click();

driver.findElement(By.linkText('Settings')).then(function(element) {
  console.log('Yes, found the element');
}, function(error) {
  console.log('The element was not found, as expected');
});
driver.quit();

它正在报错:

It is giving error:
/home/shubham/node_modules/selenium-webdriver/index.js:25
const builder = require('./builder');
^^^^^
SyntaxError: Use of const in strict mode.
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/shubham/Music/amazon_login/test_22_4_16/sel_login.js:1:79)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)

node --version v0.10.37

npm --version1.4.28

protractor --version Version 3.2.2

selenium-webdriver@2.53.1


2
你肯定想要更新你的Node.js) 或者降级Selenium驱动程序 或者使用--harmony标志启动 - tenbits
https://dev59.com/omEh5IYBdhLWcg3wRRqh#23151062 - oleh.meleshko
1
尝试运行 nodejs --harmony app.js 后,出现了 SyntaxError: Unexpected reserved word 错误。 - Shubham Batra
如果您正在使用Visual Studio并遇到此问题,请首先查看此问题/答案:https://dev59.com/VqHia4cB1Zd3GeqPbfX9 - Cos Callis
6个回答

153

更新 nodejs 解决了问题:

npm cache clean -f
sudo npm install -g n
sudo n stable
node --version
node app.js

你需要用root/administrator身份运行第二个和第三个命令。


1
我注意到你在命令行中使用了 nodejs。应该使用 node。你能打印出 "node --version" 吗? - tenbits
node --version && nodejs --version && whereis node && /usr/local/bin/node --version && sudo mv node node_0_12_2 && whereis nodejs && sudo ln -s /usr/bin/nodejs node - Torin Finnemann
以上解决方案仅更新节点版本,这将将节点版本从 v0.10.37 更新到支持 const 的更高版本,例如4或更高版本,因此错误将被解决。可以在 https://dev59.com/oqHia4cB1Zd3GeqPXK6n#44168690 找到一个正确的解决方案。 - G G
更新NodeJS可以解决这个问题。但是,在运行sudo npm install -g n之后,您可能会遇到以下错误:npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.so.10 not defined in file libssl.so.10 with link time reference为了克服这个错误,请尝试使用以下命令升级openssl:sudo yum update openssl - Dibya Sahoo

12

n个稳定的对我行不通。另一方面,

nvm install stable

实际上这让我升级到了最新的nodejs版本。显然,n stable对我来说无法通过v0.12.14。我真的不知道为什么。

注意:nvm是Node版本管理器,您可以从其github页面安装它。感谢@isaiah指出nvm不是一个已知的命令。


4

对于我来说,解决问题更简单,只需访问Node网站获取并安装LTS版本即可。


2

更新你的node版本,这样就可以解决这个问题。


0
更新NodeJS可以解决这个问题。 但是,在运行sudo npm install -g n之后,您可能会遇到以下错误:
npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.so.10 not defined in file libssl.so.10 with link time reference

为了克服这个错误,请尝试使用以下命令升级openssl:
sudo yum update openssl

-3

那个错误意味着你的节点发布量低于需求。 小心更新你的计算机节点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接