admin管理员组

文章数量:1295074

I'm trying to run a script using Puppeteer framework. (I'm a relative newbie to software). Every time I try to run, I get the error below. I'm sure nothing is wrong with the code as it worked fine an a different machine earlier. This error is noted right as main.js makes the (only) function call.

SyntaxError: Invalid or unexpected token
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._pile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
var puppeteer = require('puppeteer');
var $ = require('cheerio');
const url = '/';
​
puppeteer
  .launch()
  .then(function(browser) {
    return browser.newPage();
  })
  .then(function(page) {
    return page.goto(url).then(function() {
      return page.content();
    });
  })
  .then(function(html) {
    $('h2', html).each(function() {
        console.log($(this).text());
        console.log('\r\n');
    });
    })
  .catch(function(err) {
    //handle error
  });

I've uninstalled, cleaned, reinstalled Node.js as per this link (How do I pletely uninstall Node.js, and reinstall from beginning (Mac OS X))

I'm trying to run a script using Puppeteer framework. (I'm a relative newbie to software). Every time I try to run, I get the error below. I'm sure nothing is wrong with the code as it worked fine an a different machine earlier. This error is noted right as main.js makes the (only) function call.

SyntaxError: Invalid or unexpected token
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._pile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
var puppeteer = require('puppeteer');
var $ = require('cheerio');
const url = 'https://www.reddit./controversial/';
​
puppeteer
  .launch()
  .then(function(browser) {
    return browser.newPage();
  })
  .then(function(page) {
    return page.goto(url).then(function() {
      return page.content();
    });
  })
  .then(function(html) {
    $('h2', html).each(function() {
        console.log($(this).text());
        console.log('\r\n');
    });
    })
  .catch(function(err) {
    //handle error
  });

I've uninstalled, cleaned, reinstalled Node.js as per this link (How do I pletely uninstall Node.js, and reinstall from beginning (Mac OS X))

Share edited Apr 12, 2019 at 1:45 waterbottle asked Apr 12, 2019 at 0:31 waterbottlewaterbottle 111 gold badge1 silver badge4 bronze badges 8
  • 1 What's your Node version (node -v)? And can you post some of your code? There may be syntax that only works in newer Node versions. – Zac Anger Commented Apr 12, 2019 at 0:42
  • What version of Puppeteer are you using? there was a bug fix for a similar error: github./aslushnikov/puppeteer/mit/… – Nir Alfasi Commented Apr 12, 2019 at 0:44
  • my version of node is 10.15.3 – waterbottle Commented Apr 12, 2019 at 1:24
  • Cool, that version of Node is recent, so that's probably not the problem. To find your version of puppeteer, try cat package.json from the project root. If it doesn't appear in there, try grep version node_modules/puppeteer/package.json – Zac Anger Commented Apr 12, 2019 at 1:30
  • my version of puppeteer is 1.14.0 – waterbottle Commented Apr 12, 2019 at 1:31
 |  Show 3 more ments

1 Answer 1

Reset to default 8

This isn't actually related to puppeteer at all. On line 4, you have a zero width space character, which doesn't look like anything, but makes your Node program invalid. This sometimes shows up if you're copying a line from a site or some other source.

If you delete that line and insert a blank new line, you should be fine. I tested by copying out your code, installing the dependencies, and running it. Depending on your editor, you should be able to show unexpected unicode characters like this.

本文标签: javascriptGetting quotSyntax Error invalid or unexpected tokenquot in PuppeteerStack Overflow