Is it possible to use js variables in html while using Puppeteer?

  • Thread starter Darkmisc
  • Start date
  • Tags
    Javascript
  • #1
Darkmisc
203
27
TL;DR Summary
I'd like to use a variable from my JS file in my html file. This works fine until I use Puppeteer in the JS file. Then, it won't work.
Hi everyone

I'm using Visual Studio Code and would like to use a variable from a JavaScript file in my html code.

This is easy enough to do until I try using Puppeteer in the JS file. If I add the following line to the JS code, I can no longer call my JS variable in the html file.

Puppeteer:
const puppeteer = require("puppeteer")

I can work around this by saving the JS variable to a text file and then using the fetch method to get the data out of the file, but it's rather clunky.

fetch:
           fetch('blah.txt')
              .then(response => response.text())
              .then(data => {
                var myVariable = data;
                var paragraphElement = document.getElementById("myParagraph");
                paragraphElement.innerHTML = "wowee" + myVariable;
              })
              .catch(error => console.error(error));

Is there a more direct way to use JS variables in html while using Puppeteer?

Thanks
 
Technology news on Phys.org
  • #2
Make sure to check the console for any errors or warnings that may provide clues as to what's going wrong. Use console.log() statements in your JavaScript code to debug and trace the flow of execution. Try to isolate the problem by removing or commenting out the Puppeteer-related code from your JavaScript file and see if the variable access issue persists. This will help determine if Puppeteer is indeed causing the problem. If you're using Puppeteer's asynchronous functions with await, be mindful of the execution flow. Ensure that the variable access in your HTML file is happening after any asynchronous Puppeteer operations have completed.
 
  • Like
Likes Darkmisc
  • #3
The console says

error:
index.js:2 Uncaught ReferenceError: require is not defined
    at index.js:2:19
(anonymous) @ index.js:2

The code that it refers to is
puppeteer:
const puppeteer = require("puppeteer")

This is the part in sources that is underlined
1710716444381.png


I think flow might not be an issue here. My js variable is only there for testing. It's a fixed string. It doesn't depend on anything retrieved by Puppeteer.

EDIT: going to try fix the problem with Browserify
 
Last edited:

Similar threads

  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
2
Views
142
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
5K
Back
Top