4

I have a few javacsript files, and I'm using a js packer to pack them, then combine them all and include them in a page.

The problem is that after they are packed I'm getting this error:

Error: missing ; before statement

I assume it's because somewhere in the js file a new line is used instead of the ; character, and since the packer removes new lines you get the error

so, how could I find where ; is ommited in the script(s)?

Alex
  • 66,732
  • 177
  • 439
  • 641
  • 2
    Pass the unpacked code trough JSLint. It will tell you where to put your semi colons. However, it may also indicate dozens of other errors :) – Šime Vidas Jan 24 '11 at 16:00
  • You may want to switch compressors. YUI Compressor is good, but Google Closure Compiler may be better. Here's a dated article on the subject: http://www.bloggingdeveloper.com/post/Closure-Compiler-vs-YUI-Compressor-Comparing-the-Javascript-Compression-Tools.aspx. – Brian Donovan Jan 24 '11 at 16:07
  • thank you. found it :D (it was the cycle jquery plugin btw) – Alex Jan 24 '11 at 16:44

3 Answers3

4

There exists a tool called jslint which can statically analyse JavaScript source code with many option. It should tell you where the failure is. There is also an online version available. Check it out: http://www.jslint.com/

BenMorel
  • 34,448
  • 50
  • 182
  • 322
guido
  • 687
  • 4
  • 13
3

Depending on the tool you're using, this may happen. Imagine two .js files:

a.js

(function() {
    var bar = 10;
}())

b.js

var foo = 5;
alert(foo);

Both would work separatly, but if you pack them together, it wouldn't work anymore:

(function() { var bar = 10; }())var foo = 5;alert(foo);

obviously, because there is a missing ;. A good pattern to avoid that is to start every javascript file with a ;, like:

fixed a.js

;(function() {
    var bar = 10;
}())

fixed b.js

;var foo = 5;
alert(foo);

output

;(function() {var bar = 10;}());var foo = 5;alert(foo);

All clear, thanks!

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 2
    Wouldn't it make more sense to end every js file with a semi colon instead? – Šime Vidas Jan 24 '11 at 16:06
  • @Šime Vidas: I'm not sure whether it makes more sense or not. I like to have that right up front, so it's pretty obvious it's missing without the need to scroll to the end. – jAndy Jan 24 '11 at 16:08
  • I guess that makes sense too :) Although, I wouldn't know, since I'd never accept semi-colon-less code to begin with. – Šime Vidas Jan 24 '11 at 16:17
2

Do the files give you an error when you use them without packing? Some packers require you to place a semicolon when defining functions using a literal, and also when using object literals (otherwise they generate incorrect code -- see this):

var func = function() {
...
}; //<--- semicolon required!

var obj = {
...
}; //<--- semicolon required!

What packer are you using (JSMin and Packer don't like it if you have missing semicolons)? You can also try running your file through JSLint to see where the error exists. I suggest running the unpacked version through JSLint first (so that you can tell if there is an error with your unpacked version).

Community
  • 1
  • 1
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • 1
    @Alex Packer doesn't like it when you miss semicolons in object and function literals. Try running your original file through JSLint; you should be able to find the missing semi-colon. – Vivin Paliath Jan 24 '11 at 16:08