Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

document.styleSheets is undefined

See original GitHub issue

Is there a way to compute the document.styleSheets attribute once the window element is created and some HTML is added to it?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

capricorn86commented, May 3, 2021

@Leprosy I managed to find what caused the problem now and I have fixed it in the latest release.

I recommend using a VM context to set Happy DOM as global if you will run scripts. You can also use @happy-dom/global-registrator in test environments. I do not recommend using @happy-dom/global-registrator in environments where you do not have control over the page HTML. If you plan to execute unknown code I recommend to use a VM context and disable script evaluation in the VM as it is always a risk that the script manages to jump out from the VM somehow.

I have also added support for the window “load” event and the document “readystatechange” event, but as your code seems to execute scripts I recommend using happyDOM.whenAsyncComplete() as it will also wait for the script to finish executing (timeouts, fetches etc.).

You can read more about the latest releases here:

I used this script for testing:

const { Window } = require('./packages/happy-dom');
const VM = require('vm');

async function getStyles() {
    const window = VM.createContext(new Window());
    const document = window.document;
    window.location.href = '';
    document.write(`<!DOCTYPE html>
          h1 { text-decoration: underline; }
        <link rel="stylesheet" href="style.css">
        <h1> This is test </h1>
        <p>This has some dynamic generated content</p>
        <p>This should be enought to test</p>
        <img src="celes.jpeg" width="50" height="100" />
        <img src="img/guitar.jpeg" width="50" height="100" />
          const getQuote = async () => {
            let text = "";
            try {
              const res = await fetch("");
              text = await res.text();
            } catch(e) {
              text = \`Error fetching: \${e}\`;
            } finally {
              document.getElementsByTagName("h2")[0].innerHTML = text
          setTimeout(getQuote, 2000);

    await window.happyDOM.whenAsyncComplete();

    return document.styleSheets;

getStyles().then(styleSheets => {
    let index = 0;
    for(const styleSheet of styleSheets) {
        console.log('Stylesheet #' + index + ':');
        for(const cssRule of styleSheet.cssRules) {
}).catch(error => {
capricorn86commented, Apr 30, 2021

@Leprosy the code examples I sent is for server side. However, I am working on adding support for the “load” and “readystatechange” event that you will be able to use instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trying to read styleSheets returns undefined - Stack Overflow
I'm trying to read cssText of the first stylesheet using document.styleSheets[0].cssText , but it always returns undefined .
Read more >
stylesheet variable is undefined in Chrome · Issue #223 - GitHub
The issue seems to be that Chrome doesn't update the documents.styleSheets variable right after the text node with the CSS rules has been...
Read more >
document.styleSheets[0] is undefined [#362735] |
I am getting this error in FF Error: document.styleSheets[0] is undefined .../modules/dynamic_views/js/dynamic_views.js line 229.
Read more >
Document.styleSheets - Web APIs | MDN
The styleSheets read-only property of the Document interface returns a StyleSheetList of CSSStyleSheet objects, for stylesheets explicitly ...
Read more >
CSSStyleDeclaration parentRule Property - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found