【问题标题】:Async throwing SyntaxError: Unexpected token (异步抛出 SyntaxError: Unexpected token (
【发布时间】:2018-02-14 09:58:49
【问题描述】:

我正在使用无头 Chrome 包 Puppeteer 运行测试:

const puppeteer = require('puppeteer')

;(async() => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://google.com', {waitUntil: 'networkidle'})
  // Type our query into the search bar
  await page.type('puppeteer')

  await page.click('input[type="submit"]')

  // Wait for the results to show up
  await page.waitForSelector('h3 a')

  // Extract the results from the page
  const links = await page.evaluate(() => {
    const anchors = Array.from(document.querySelectorAll('h3 a'))
    return anchors.map(anchor => anchor.textContent)
  })
  console.log(links.join('\n'))
  browser.close()
})()

我将脚本运行为:node --harmony test/e2e/puppeteer/index.js (v6.9.1)

但我收到此错误:

;(async() => {
       ^
SyntaxError: Unexpected token (

可能是什么问题?

注意:我使用的是 Vue CLI 的官方 Webpack 模板:

【问题讨论】:

标签: javascript webpack vue.js ecmascript-2017 puppeteer


【解决方案1】:

经过 lint 之后,我在笔记本电脑上尝试了您的代码,它运行良好:

也许你有环境问题。

您是否考虑过删除行首的分号? 它看起来不像正确的编程。或者可能是 webpack 问题。

【讨论】:

  • 您使用的是哪个版本的节点?
  • 我使用的是最新版本,目前是 8.4.0
【解决方案2】:

我发现:节点 LTS(AK​​A 节点 6)现在不支持异步/等待机制。看 :

详情请看这里:https://www.infoq.com/news/2017/02/node-76-async-await

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2019-02-10
    • 2019-11-10
    相关资源
    最近更新 更多