Error: Evaluation failed: SyntaxError: Unexpected identifier
JaLe29 opened this issue · comments
Hi,
evaluate function doesn't work correctly in the compiled file.
Example code:
const path = require('path');
const puppeteer = require('puppeteer');
const isPkg = typeof process.pkg !== 'undefined';
//mac path replace
let chromiumExecutablePath = (isPkg ?
puppeteer.executablePath().replace(
/^.*?\/node_modules\/puppeteer\/\.local-chromium/,
path.join(path.dirname(process.execPath), 'chromium')
) :
puppeteer.executablePath()
);
console.log(process.platform)
//check win32
if (process.platform == 'win32') {
chromiumExecutablePath = (isPkg ?
puppeteer.executablePath().replace(
/^.*?\\node_modules\\puppeteer\\\.local-chromium/,
path.join(path.dirname(process.execPath), 'chromium')
) :
puppeteer.executablePath()
);
}
(async () => {
console.log(chromiumExecutablePath)
const browser = await puppeteer.launch({
executablePath: chromiumExecutablePath,
headless: false
});
const page = await browser.newPage()
await page.goto('https://www.google.com')
const res = await page.evaluate(() => {
const height = screen.height
const width = screen.width
return {height, width}
}).catch((e) => console.log(e))
console.log(res)
await browser.close()
console.log('Exit...')
})()
Compiled fine.
After run the exe file I am getting a error:
C:\Users\xxx\Desktop\pupptr-test>index-win.exe
win32
C:\Users\xxx\Desktop\pupptr-test\chromium\win64-594312\chrome-win\chrome.exe
Error: Evaluation failed: SyntaxError: Unexpected identifier
at ExecutionContext.evaluateHandle (C:\snapshot\Desktop\pupptr-test\node_modules\puppeteer\lib\ExecutionContext.js:106:13)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at ExecutionContext. (C:\snapshot\Desktop\pupptr-test\node_modules\puppeteer\lib\helper.js:144:27)
at ExecutionContext.evaluate (C:\snapshot\Desktop\pupptr-test\node_modules\puppeteer\lib\ExecutionContext.js:58:31)
at ExecutionContext. (C:\snapshot\Desktop\pupptr-test\node_modules\puppeteer\lib\helper.js:145:23)
at Frame.evaluate (C:\snapshot\Desktop\pupptr-test\node_modules\puppeteer\lib\FrameManager.js:439:20)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at Frame. (C:\snapshot\Desktop\pupptr-test\node_modules\puppeteer\lib\helper.js:144:27)
at Page.evaluate (C:\snapshot\Desktop\pupptr-test\node_modules\puppeteer\lib\Page.js:728:43)
at Page. (C:\snapshot\Desktop\pupptr-test\node_modules\puppeteer\lib\helper.js:145:23)
at C:\snapshot\Desktop\pupptr-test\index.js:0:0
at process._tickCallback (internal/process/next_tick.js:68:7)
undefined
closing
What is wrong?
You can check this issue, it may be helpful.
That is an import module issue.
puppeteer/puppeteer#2267 @JaLe29
and this repository https://github.com/GoogleChromeLabs/carlo/blob/master/lib/carlo.js
@JaLe29 I think you have a problem here:
const res = await page.evaluate(() => {
const height = screen.height
const width = screen.width
return {height, width}
}).catch((e) => console.log(e))
You cannot use function literals with page.evaluate()
. Try using a string like so:
const res = await page.evaluate(`
const height = screen.height
const width = screen.width
return {height, width}
`).catch((e) => console.log(e))
Puppeteer uses an API to communicate with Chrome internally. It needs to somehow communicate the code to execute on the page. Normally, it uses func.toString()
, which works like so...
function fib(n) {
if (n === 0) return 1
if (n === 1) return 1
return fib(n - 2) + fib(n - 1)
}
fib.toString()
// 'function fib(n) {\nif (n === 0) return 1\nif (n === 1) return 1\nreturn fib(n - 2) + fib(n - 1)\n}'
However, via pkg, func.toString()
returns '[native code]'
, not the actual source. So you have to provide the source code manually as a string.