uncss: how to deal with 'ReferenceError: Can't find variable: $' error

It was working fine for some files but now all of sudden, getting this error

ReferenceError: Can't find variable: $

how to fix it?

atleast if i know, on which file i am getting this error…i will be able to fix it.

thanks.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 29 (16 by maintainers)

Most upvoted comments

Unfortunately, this is an issue that happens for so long and personally I can’t figure out why.

The solution is to either use an older uncss version (0.12.1 doesn’t throw those errors), or stop inlining JS code.

Well in my case moving all JS to external files fixed the errors.

Plus all the advantages of doing so.

Ideally the issue should be fixed but I’ve yet to make a small test case.

I’ll try to do it later or tomorrow. On May 24, 2016 21:59, “lorvent” notifications@github.com wrote:

hmm nice tip but that doesn’t serve real purpose as its just hiding errors,

whereas my whole idea of using build systems is to improve code quality…

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/giakki/uncss/issues/232#issuecomment-221365539

Found the same issue. This minimal example reproduces the error.

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" async="async"></script>
    <link rel="stylesheet" media="all" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />

    <script>
      function a() {
        $('#div').show();
      }
      a();
    </script>
  </head>
  <body>
  </body>
</html>

hmm nice tip but that doesn’t serve real purpose as its just hiding errors,

whereas my whole idea of using build systems is to improve code quality…

Sigh. These are probably best discussed on another bug. I am not clear why you’re trying to discuss a hodgepodge of random complaints in this bug, since AFAICT this bug has nothing to do with these two issues you just brought up?

Can you please file individual requests, one for each independent issue, to help keep this project clean and coherent?

I will be happy to respond to your complaints (to explain why they are impossible and/or detrimental to your own stated end goals) in any individual bugs you file, since it does not make sense to do this here, as that will only continue to spam the others in this thread who do not care about your personal bugs.

Thanks

The above HTML file uses an async sync, meaning the calling page will not block to wait for that script to load before loading/running the rest of the page. So when the call to $('#div') occurs, there is no guarantee that the jquery code has been loaded yet. And if the jquery code has not been loaded, I would expect you to get an error very similar to Can't find variable: $.

So from my perspective, the above HTML file is broken code, and uncss/phantom is just errorring on the broken code.