New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to deal with 'ReferenceError: Can't find variable: $' error #232
Comments
I'm guessing you forgot to include jQuery in one of your html files. You could try opening each page in the browser and looking at the console. |
I don't think so because everypage uses many jquery plugins including bootstrap so that should not be the issue but can you help me finding out more information? like using plumber to get file name etc... |
Not sure. Perhaps @XhmikosR, @mikelambert, or @ben-eb could help you better. |
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. |
I'll say the same thing I said in #220, if you want to give a relatively small reproducible test case, it's possible someone can look into it. My guess as to the reason 0.12.1 doesn't throw these errors (this is for you too @XhmikosR) is that 0.21 just silently drops the warnings. This particular change was only included in 0.13: 62145b3 So you can downgrade, but I suspect you're really just hiding existing errors by doing so. |
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... |
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.
|
Found the same issue. <!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> |
Confirmed. I'm getting ready to do a git bisect to find this bug. @jogaco Thanks for posting! |
in mycase, if you add js in html page we will get this problem, |
@lorvent Yes, moving JS to a separate file fixes it, but uncss should support inline JS. I'm currently running |
And the results are:
@mikelambert You were right:
The first bad commit is 62145b3. |
@XhmikosR Is this a PhantomJS bug? Perhaps ariya/phantomjs#14422 or ariya/phantomjs#14316? |
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 So from my perspective, the above HTML file is broken code, and uncss/phantom is just errorring on the broken code. |
That is correct. my example in #220 has a snippet which doesn't use async that triggers the On Jul 30, 2016 11:26 PM, "Mike Lambert" notifications@github.com wrote:
|
Well the minimal repro example above has a bug, which uncss found. Your example in #220 is probably due to a different issue then (though due to the same issue of a jquery file not being loaded properly). I'll try to respond with some ideas in that thread. |
What about this?
|
You are still missing the point. You are using async. |
You are still referencing the $ variable before jQuery is loaded, so you will still get that error. You can either write code that doesn't use $ until after the page is loaded (which your example tried but did not do), or you can stop using async (and ideally move your <script> tags out of your ) |
Got it! |
Oops, didn't look at the test case close enough. |
Can anyone check if this is fixed with uncss 0.15.0? We switched to jsdom in this release. |
Doesn't seem to throw even with But the test case from #220 (comment) still throws because there the file is local. I even tried with |
Err scratch that. It works fine. I need to sleep :/ |
Although, it still misses the JavaScript added selectors. This is still an issue, right? |
I'm not sure what you mean by 'js added selectors'. I don't see that mentioned in this issue? If you're talking about classes that get added to the page at runtime in response to user events, then yes, it misses those. I consider this a "Will Not Fix" bug though. |
Why is that? It's a pretty common use case and almost every single website does this. On a side note if you try my example from #220 (comment) with the latest version, it even misses the inline styles. <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>uncss TypeError demo</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
<style>
.content {
margin: 20px;
}
.red {
color: red;
}
.green {
color: green;
}
</style>
</head>
<body>
<div class="content">
<span class="green">This text should be green and change to red after page load.</span>
</div>
<script src="jquery.slim.min.js"></script>
<script>
$(function() {
$(".green").removeClass("green").addClass("red");
});
</script>
</body>
</html> output css /*** uncss> filename: https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css ***/html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}::-webkit-input-placeholder{color:inherit;opacity:.54}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}/*# sourceMappingURL=normalize.min.css.map */
|
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 |
For everyone still having this error, this could be happening to you: #268 (comment) |
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.
The text was updated successfully, but these errors were encountered: