I have a puppeteer script that works on my local PC. I'd like to host the script on my CPanel so that I can run it every day through my namecheap hosting service. I installed a nodejs app on my CPanel, and I try to run my code but I get this error. I am trying to scrape some numbers from the internet and then upload them into mySQL. Keep in mind I have no idea what I'm actually doing.
gaspricesapi@1.0.0 start /home/sameryalda/GasPricesAPI
node app.js
stderr:
npm WARN lifecycle The node binary used for scripts is
/home/sameryalda/nodevenv/GasPricesAPI/10/bin/node but npm is using
/opt/alt/alt-
nodejs10/root/usr/bin/node itself. Use the --scripts-prepend-node-
path option to include
the path for the node binary npm was executed with.
/home/sameryalda/GasPricesAPI/app.js:1
import puppeteer from 'puppeteer';
^^^^^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js
(internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain
(internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gaspricesapi@1.0.0 start: node app.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gaspricesapi@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely
additional logging
output above.`
nodejs app setup:
import puppeteer from 'puppeteer';
import fetch from 'node-fetch';
import mysql from 'mysql';
let con = mysql.createConnection({
host: "localhost",
user: "test",
password: "test"
});
let scrapeAvgGas = async (url) => {
let average = 0;
let numRecords = 0;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
for(let i = 0; (await page.$x(`//[@id="spnGB27562Price${i}"]`)) != ''; i++) {
const [el] = await page.$x(`//[@id="spnGB27562Price${i}"]`);
const txt = await el.getProperty('textContent');
let rawTxt = await txt.jsonValue();
rawTxt = Number(rawTxt.replace(/[^\d.-]/g, ''));
average += rawTxt;
numRecords++;
}
return Math.round(((average / numRecords) + Number.EPSILON) * 100) / 100
}
let scrapeBridgeGas = async (url, xpath) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const [el] = await page.$x(xpath);
const txt = await el.getProperty('textContent');
let rawTxt = await txt.jsonValue();
rawTxt = Number(rawTxt.replace(/[^\d.-]/g, ''));
return rawTxt;
}
async function getExchange(url) {
try {
const response = await fetch(url, {
method: 'GET',
credentials: 'same-origin'
});
const exch = await response.json();
return exch.observations[0].FXUSDCAD.v;
} catch (error) {
console.error(error);
}
}
let gasWindsor = await scrapeAvgGas();
let gasBridge = await
scrapeBridgeGas('https://www.ambassadorbridge.com/fuel-price-
list/', '//*[@id="qodef-page-content"]/div/div/p/text()[4]')
let premBridge = await
scrapeBridgeGas('https://www.ambassadorbridge.com/fuel-price-
list/', '//*[@id="qodef-page-content"]/div/div/p/text()[5]')
let exchRate = await
getExchange('https://www.bankofcanada.ca/valet/observations/FXUSDCAD/json?recent=1')
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
let sql = "INSERT INTO customers (name, price) VALUES ?";
let values = [
['gasWindsor', gasWindsor],
['gasBridge', gasBridge],
['premBridge', premBridge],
['exchRate', exchRate],
];
con.query(sql, [values], function (err, result) {
if (err) throw err;
console.log("Number of records inserted: " + result.affectedRows);
});
});
