Headless Chrome browser fail to launch on windows container? - node.js

I'm using windows nanoserver container to launch puppeteer but it fails and I'm not sure what's wrong with my code
here's my docker file
FROM mcr.microsoft.com/windows/nanoserver:1903
COPY nodejs /windows/system32
CMD [ "node.exe" ]
COPY package.json ./
RUN npm install
# Bundle app source
COPY . .
ENTRYPOINT [ "node", "./server.js" ]
nodejs is a the node folder on my machine
and here's my package.json
{
"dependencies": {
"puppeteer": "^5.5.0",
"#axe-core/puppeteer": "^4.1.0"
}
}
and here's my server.js
const { AxePuppeteer } = require('#axe-core/puppeteer');
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: true,
ignoreDefaultArgs: ['--disable-extensions'],
args: ['--disable-dev-shm-usage', '--no-sandbox', '--disable-gpu', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
await page.goto('https://stackoverflow.com/');
const results = await new AxePuppeteer(page).analyze();
console.log(results);
await page.close();
await browser.close();
})();
So if I build the docker image then run
docker build -t chromeHeadless
docker run chromeHeadless
I get this error
(node:1228) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (C:\app\node_modules\puppeteer\lib\cjs\puppeteer\node\BrowserRunner.js:193:20) at Interface.<anonymous> (C:\app\node_modules\puppeteer\lib\cjs\puppeteer\node\BrowserRunner.js:183:68)
at Interface.emit (events.js:326:22)
at Interface.close (readline.js:424:8)
at Socket.onend (readline.js:202:10)
at Socket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1244:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1228) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1228) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Related

Error of launching chrome-linux with puppeteer in Centos

I use puppeteer-core with chrome-linux from https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1095492/chrome-linux.zip.
this is my project:
project directory
// this is index.js
const puppeteer = require('puppeteer-core');
(async () => {
const browser = await puppeteer.launch({ headless: true, executablePath: './chrome-linux/chrome', args: ['--no-sandbox'] });
const page = await browser.newPage();
await page.goto('https://www.baidu.com/');
// Set screen size
await page.setViewport({ width: 1080, height: 1024 });
await browser.close();
})();
I excute node index.js, then the error came out.
(node:32513) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
./chrome-linux/chrome: symbol lookup error: ./chrome-linux/chrome: undefined symbol: gbm_bo_get_modifier
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/opt/xxx/example/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:299:20)
at Interface.<anonymous> (/opt/xxx/example/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:287:24)
at Interface.emit (events.js:412:35)
at Interface.close (readline.js:530:8)
at Socket.onend (readline.js:254:10)
at Socket.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1333:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:32513) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:32513) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I could not launch Chrome. How can I fix this error? Thanks.
Puppeteer version
19.7.0
Node version
14.21.1
Package manager
npm
Package manager version
6.14.17
Operating system
Linux

Node.js Problem with puppeteer headless:True : " Evaluation failed: TypeError: Cannot read property 'querySelectorAll' of null"

My code works perfectly well with headless false, but when I change it to true I get this error :
(node:92024) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'querySelectorAll' of null
at __puppeteer_evaluation_script__:7:17
at ExecutionContext._evaluateInternal (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async ExecutionContext.evaluate (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16)
at async getAllInfos (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\index.js:28:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:92024) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:92024) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
My code :
async function getAllInfos() {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
page.setDefaultNavigationTimeout(0);
await page.goto(`https://www.japanread.cc/`, {
waitUntil: ["load", "domcontentloaded", "networkidle0", "networkidle2"],
}); //se rendre sur une page
let trs = [];
trs = tbody.querySelectorAll("tr");//the error is here
I tried to put showMo, put several things in the "waitUntil" but I get nothing, I really don't understand.
Do you know what the problem is?

nodejs program to run puppyteerjs fails

I have written a very simple puppyteerjs program :
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false,
slowMo: 250 });
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
PS D:\js\puppe> node .\screenshot.js
(node:2052) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md
at onClose (D:\js\puppe\node_modules\puppeteer\lib\Launcher.js:750:14)
at ChildProcess.helper.addEventListener (D:\js\puppe\node_modules\puppeteer\lib\Launcher.js:740:61)
at ChildProcess.emit (events.js:194:15)
at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
(node:2052) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2052) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I tried this program with node version 12 and node version 10, I tried with a headless flag and also giving the path of chrome.exe file from node modules but it keeps on throwing the same exception.
My current environment is: windows 10 and node 10

Chrome launcher exits with UnhandledPromiseRejectionWarning

I am trying to set up chrome-launcher to output all console messages to terminal. My code looks like this
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
(async function() {
async function launchChrome() {
return await chromeLauncher.launch({
chromeFlags: [
'--window-size=1200,800',
'--user-data-dir=/tmp/chrome-testing',
'--auto-open-devtools-for-tabs'
]
});
}
const chrome = await launchChrome();
const protocol = await CDP({
port: chrome.port
});
const {
DOM,
Network,
Page,
Runtime,
Console
} = protocol;
await Promise.all([Network.enable(), Page.enable(), DOM.enable(), Runtime.enable(), Console.enable()]).catch(console.log);
// REMARKS: messageAdded is fired every time a new console message is added
Console.messageAdded((result) => {
console.log(result);
});
})();
I copied some of this from the question here: How to get console.log output in Terminal via Headless Chrome Runtime.evaluate
When I try to navigate to a page, none of the console messages show up in the terminal, and the chrome-launcher exits with the following error:
(node:14531) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:64656
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1083:14)
(node:14531) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14531) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Running Vue dev, giving me promise rejection

When I run vue's npm run dev, I got this error:
(node:10871) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Exited with code 3
It works fine if I run in my computer with Node 6.11.3
But fails when I deploy to AWS EC2 with Node 6.11.3, or the Latest Node 9.something
If I run with latest node, I got a detail error message:
(node:10429) UnhandledPromiseRejectionWarning: Error: Exited with code 3
at ChildProcess.cp.once.code (/home/ubuntu/loan-vue-frontend/node_modules/opn/index.js:84:13)
at Object.onceWrapper (events.js:272:13)
at ChildProcess.emit (events.js:180:13)
at maybeClose (internal/child_process.js:936:16)
at Socket.stream.socket.on (internal/child_process.js:353:11)
at Socket.emit (events.js:180:13)
at Pipe._handle.close [as _onclose] (net.js:541:12)
(node:10429) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10429) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I have tried just installing a new project from scratch and it works.
Then I compare the code, and I found out I have this code (in a file called 'build/dev-server.js' :
var _resolve
var readyPromise = new Promise(resolve => {
_resolve = resolve
})
console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
console.log('> Listening at ' + uri + '\n')
// when env is testing, don't need open it
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
opn(uri)
}
_resolve()
})
var server = app.listen(port)
module.exports = {
ready: readyPromise,
close: () => {
server.close()
}
}
It seems to me the readyPromise is the cause of trouble.
But the only place that call that dev-server.js is in package.json:
"scripts": {
"dev": "node build/dev-server.js",
}
I suspect
ready: readyPromise,
is not right because 'ready' may not have a catch block. but I have no clue how I can fix it if that's the case.
Any idea?

Resources