Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A Tackling net::ERR_NAME_NOT_RESOLVED and timeout error on browser object creation when using Puppeteer

To fix TypeError: Cannot read properties of undefined (reading 'close'), remove the const in the following code, which ensures your let browser outside the block will be assigned in the common case...

posted 7d ago by ggorlen‭  ·  edited 7d ago by Alexei‭

Answer
#5: Post edited by user avatar Alexei‭ · 2024-12-15T20:51:28Z (7 days ago)
fixed broken link
  • To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned in the common case when `browser.launch()` doesn't throw:
  • ```diff
  • - const browser = await puppeteer.launch({
  • + browser = await puppeteer.launch({
  • ```
  • But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:
  • ```diff
  • - await browser.close();
  • + await browser?.close();
  • ```
  • Also, as you're seeing, the
  • ```js
  • console.warn("Could not create a browser instance: ", err);
  • ```
  • log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:
  • ```diff
  • - console.warn("Could not create a browser instance: ", err);
  • + console.error(err);
  • ```
  • ---
  • As for your other question, the following listener forwards all logs from the website console into your Node console:
  • ```js
  • page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
  • ```
  • The output you're seeing is mostly harmless noise logged to a greater or []()lesser extent from site to site. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.
  • Generally speaking, I recommend the following goto:
  • ```js
  • await page.goto(url, {waitUntil: "domcontentloaded"});
  • ```
  • which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).
  • Furthermore, the many sites take anti-bot measures, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent
  • ```js
  • const ua =
  • "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.3";
  • await page.setUserAgent(ua);
  • ```
  • If that doesn't work, you can try the [Puppeteer stealth plugin](https://www.npmjs.com/package/puppeteer-extra-plugin-stealth[]()) or other fingerprint obfuscating measures. Or use headful mode.
  • Note that browser automation has few silver bullets, so a script that can automate one site may not work on another. You'll generally need to adapt your code a bit to determine when a particular page is fully loaded or otherwise in an actionable state, depending on what you want to accomplish on it.
  • To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned in the common case when `browser.launch()` doesn't throw:
  • ```diff
  • - const browser = await puppeteer.launch({
  • + browser = await puppeteer.launch({
  • ```
  • But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:
  • ```diff
  • - await browser.close();
  • + await browser?.close();
  • ```
  • Also, as you're seeing, the
  • ```js
  • console.warn("Could not create a browser instance: ", err);
  • ```
  • log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:
  • ```diff
  • - console.warn("Could not create a browser instance: ", err);
  • + console.error(err);
  • ```
  • ---
  • As for your other question, the following listener forwards all logs from the website console into your Node console:
  • ```js
  • page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
  • ```
  • The output you're seeing is mostly harmless noise logged to a greater or []()lesser extent from site to site. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.
  • Generally speaking, I recommend the following goto:
  • ```js
  • await page.goto(url, {waitUntil: "domcontentloaded"});
  • ```
  • which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).
  • Furthermore, the many sites take anti-bot measures, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent
  • ```js
  • const ua =
  • "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.3";
  • await page.setUserAgent(ua);
  • ```
  • If that doesn't work, you can try the [Puppeteer stealth plugin](https://www.npmjs.com/package/puppeteer-extra-plugin-stealth) or other fingerprint obfuscating measures. Or use headful mode.
  • Note that browser automation has few silver bullets, so a script that can automate one site may not work on another. You'll generally need to adapt your code a bit to determine when a particular page is fully loaded or otherwise in an actionable state, depending on what you want to accomplish on it.
#4: Post edited by user avatar ggorlen‭ · 2024-12-15T18:19:57Z (7 days ago)
clarify
  • To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned in the common case when `browser.launch()` doesn't throw:
  • ```diff
  • - const browser = await puppeteer.launch({
  • + browser = await puppeteer.launch({
  • ```
  • But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:
  • ```diff
  • - await browser.close();
  • + await browser?.close();
  • ```
  • Also, as you're seeing, the
  • ```js
  • console.warn("Could not create a browser instance: ", err);
  • ```
  • log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:
  • ```diff
  • - console.warn("Could not create a browser instance: ", err);
  • + console.error(err);
  • ```
  • ---
  • As for your other question, the following listener forwards all logs from the website console into your Node console:
  • ```js
  • page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
  • ```
  • The output you're seeing is mostly harmless noise logged to a greater or []()lesser extent from site to site. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.
  • Generally speaking, I recommend the following goto:
  • ```js
  • await page.goto(url, {waitUntil: "domcontentloaded"});
  • ```
  • which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).
  • Furthermore, the many sites take anti-bot measures, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent. If that doesn't work, you can try the Puppeteer stealth plugin or other fingerprint obfuscating measures. Or use headful mode.
  • Note that browser automation has few silver bullets, so a script that can automate one site may not work on another. You'll generally need to adapt your code a bit to determine when a particular page is fully loaded or otherwise in an actionable state, depending on what you want to accomplish on it[]().
  • To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned in the common case when `browser.launch()` doesn't throw:
  • ```diff
  • - const browser = await puppeteer.launch({
  • + browser = await puppeteer.launch({
  • ```
  • But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:
  • ```diff
  • - await browser.close();
  • + await browser?.close();
  • ```
  • Also, as you're seeing, the
  • ```js
  • console.warn("Could not create a browser instance: ", err);
  • ```
  • log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:
  • ```diff
  • - console.warn("Could not create a browser instance: ", err);
  • + console.error(err);
  • ```
  • ---
  • As for your other question, the following listener forwards all logs from the website console into your Node console:
  • ```js
  • page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
  • ```
  • The output you're seeing is mostly harmless noise logged to a greater or []()lesser extent from site to site. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.
  • Generally speaking, I recommend the following goto:
  • ```js
  • await page.goto(url, {waitUntil: "domcontentloaded"});
  • ```
  • which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).
  • Furthermore, the many sites take anti-bot measures, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent
  • ```js
  • const ua =
  • "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.3";
  • await page.setUserAgent(ua);
  • ```
  • If that doesn't work, you can try the [Puppeteer stealth plugin](https://www.npmjs.com/package/puppeteer-extra-plugin-stealth[]()) or other fingerprint obfuscating measures. Or use headful mode.
  • Note that browser automation has few silver bullets, so a script that can automate one site may not work on another. You'll generally need to adapt your code a bit to determine when a particular page is fully loaded or otherwise in an actionable state, depending on what you want to accomplish on it.
#3: Post edited by user avatar ggorlen‭ · 2024-12-15T18:17:52Z (7 days ago)
tweak
  • To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned in the common case when `browser.launch()` doesn't throw:
  • ```diff
  • - const browser = await puppeteer.launch({
  • + browser = await puppeteer.launch({
  • ```
  • But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:
  • ```diff
  • - await browser.close();
  • + await browser?.close();
  • ```
  • Also, as you're seeing, the
  • ```js
  • console.warn("Could not create a browser instance: ", err);
  • ```
  • log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:
  • ```diff
  • - console.warn("Could not create a browser instance: ", err);
  • + console.error(err);
  • ```
  • ---
  • As for your other question, the following listener forwards all logs from the website console into your Node console:
  • ```js
  • page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
  • ```
  • The output you're seeing is mostly harmless noise logged to a greater or lesser extent from one site to another. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.
  • Generally speaking, I recommend the following goto:
  • ```js
  • await page.goto(url, {waitUntil: "domcontentloaded"});
  • ```
  • which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).
  • Furthermore, the many sites take anti-bot measures, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent. If that doesn't work, you can try the Puppeteer stealth plugin or other fingerprint obfuscating measures. Or use headful mode.
  • Note that browser automation has few silver bullets, so a script that can automate one site may not work on another. You'll generally need to adapt your code a bit to determine when a particular page is fully loaded or otherwise in an actionable state, depending on what you want to accomplish on it[]().
  • To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned in the common case when `browser.launch()` doesn't throw:
  • ```diff
  • - const browser = await puppeteer.launch({
  • + browser = await puppeteer.launch({
  • ```
  • But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:
  • ```diff
  • - await browser.close();
  • + await browser?.close();
  • ```
  • Also, as you're seeing, the
  • ```js
  • console.warn("Could not create a browser instance: ", err);
  • ```
  • log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:
  • ```diff
  • - console.warn("Could not create a browser instance: ", err);
  • + console.error(err);
  • ```
  • ---
  • As for your other question, the following listener forwards all logs from the website console into your Node console:
  • ```js
  • page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
  • ```
  • The output you're seeing is mostly harmless noise logged to a greater or []()lesser extent from site to site. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.
  • Generally speaking, I recommend the following goto:
  • ```js
  • await page.goto(url, {waitUntil: "domcontentloaded"});
  • ```
  • which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).
  • Furthermore, the many sites take anti-bot measures, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent. If that doesn't work, you can try the Puppeteer stealth plugin or other fingerprint obfuscating measures. Or use headful mode.
  • Note that browser automation has few silver bullets, so a script that can automate one site may not work on another. You'll generally need to adapt your code a bit to determine when a particular page is fully loaded or otherwise in an actionable state, depending on what you want to accomplish on it[]().
#2: Post edited by user avatar ggorlen‭ · 2024-12-15T18:17:00Z (7 days ago)
clarify
  • To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned if `browser.launch()` doesn't throw:
  • ```diff
  • - const browser = await puppeteer.launch({
  • + browser = await puppeteer.launch({
  • ```
  • But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:
  • ```diff
  • - await browser.close();
  • + await browser?.close();
  • ```
  • ----
  • Also, as you're seeing, the
  • ```js
  • console.warn("Could not create a browser instance: ", err);
  • ```
  • log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:
  • ```diff
  • - console.warn("Could not create a browser instance: ", err);
  • + console.error(err);
  • ```
  • ---
  • As for your other question, the following listener forwards all logs from the website console into your Node console:
  • ```js
  • page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
  • ```
  • The output you're seeing is mostly harmless noise from the website. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.
  • Generally speaking, I recommend the following goto:
  • ```js
  • await page.goto(url, {waitUntil: "domcontentloaded"});
  • ```
  • which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).
  • The chunkbase site probably uses anti-botting, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent. If that doesn't work, you can try the Puppeteer stealth plugin or other fingerprint obfuscating measures. Or use headful mode.
  • To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned in the common case when `browser.launch()` doesn't throw:
  • ```diff
  • - const browser = await puppeteer.launch({
  • + browser = await puppeteer.launch({
  • ```
  • But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:
  • ```diff
  • - await browser.close();
  • + await browser?.close();
  • ```
  • ----
  • Also, as you're seeing, the
  • ```js
  • console.warn("Could not create a browser instance: ", err);
  • ```
  • log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:
  • ```diff
  • - console.warn("Could not create a browser instance: ", err);
  • + console.error(err);
  • ```
  • ---
  • As for your other question, the following listener forwards all logs from the website console into your Node console:
  • ```js
  • page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
  • ```
  • The output you're seeing is mostly harmless noise logged to a greater or lesser extent from one site to another. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.
  • Generally speaking, I recommend the following goto:
  • ```js
  • await page.goto(url, {waitUntil: "domcontentloaded"});
  • ```
  • which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).
  • Furthermore, the many sites take anti-bot measures, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent. If that doesn't work, you can try the Puppeteer stealth plugin or other fingerprint obfuscating measures. Or use headful mode.
  • Note that browser automation has few silver bullets, so a script that can automate one site may not work on another. You'll generally need to adapt your code a bit to determine when a particular page is fully loaded or otherwise in an actionable state, depending on what you want to accomplish on it[]().
#1: Initial revision by user avatar ggorlen‭ · 2024-12-15T18:12:48Z (7 days ago)
To fix `TypeError: Cannot read properties of undefined (reading 'close')`, remove the `const` in the following code, which ensures your `let browser` outside the block will be assigned if `browser.launch()` doesn't throw:

```diff
- const browser = await puppeteer.launch({
+ browser = await puppeteer.launch({
```

But if `browser.launch()` throws, then `let browser` will be undefined, so a throw is still possible in the `finally` block. Handle this in your `finally` block by using optional chaining:

```diff
- await browser.close();
+ await browser?.close();
```

---

Also, as you're seeing, the

```js
console.warn("Could not create a browser instance: ", err);
```

log is misleading, since you're clearly able to create a browser instance. This `catch` block will run if any errors throw within the main automation block, which could be more than just failing to create a browser instance. That's only the first line of the block. I would make it:

```diff
- console.warn("Could not create a browser instance: ", err);
+ console.error(err);
```

---

As for your other question, the following listener forwards all logs from the website console into your Node console:

```js
page.on('console', (msg) => console.log('DEBUG:', msg.text()) );
```

The output you're seeing is mostly harmless noise from the website. I generally wouldn't add this listener since most large sites spam the console with random resource loading failures like this.

Generally speaking, I recommend the following goto:

```js
await page.goto(url, {waitUntil: "domcontentloaded"});
```

which is least likely to get stuck and throw a loading timeout. If you're taking a screenshot, wait for all images to load manually before doing so (`"networkidle0"` can get stuck, as can the default `"load"` predicate).

The chunkbase site probably uses anti-botting, and headless mode makes bots much easier to detect. The default user agent basically says "I am a bot", so you can start by setting it to a normal mobile browser user agent. If that doesn't work, you can try the Puppeteer stealth plugin or other fingerprint obfuscating measures. Or use headful mode.