Find error in javascript

I have several javacsript files, and I use the js package to pack them, and then merge them and include them on the page.

The problem is that after they are packed, I get this error:

Error: missing; before expression

I guess this is because somewhere in the js file instead of a character ; a new line is used, and since the packer deletes new lines, you get an error

So how can I find where ; omitted in script (s)?

+4
source share
3 answers

There is a jslint tool that can statically analyze JavaScript source code with many parameters. He must tell you where the failure occurred. There is also an online version. Check it out: http://www.jslint.com/

+3
source

Depending on the tool used, this can happen. Imagine two .js files:

a.js

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

b.js

 var foo = 5; alert(foo); 

Both will work separately, but if you pack them together, this will not work anymore:

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

obviously because it is absent ; . A good template to avoid this is to run each javascript file with ; , eg:

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); 

Everything is clear, thanks!

+3
source

Do files make an error message if you use them without packaging? Some packers require a semicolon when defining functions using a literal, as well as when using object literals (otherwise they generate the wrong code - see this ):

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

Which packer do you use (JSMin and Packer do not like it if you have semicolons)? You can also try running the file through JSLint to find out where the error is. I suggest that you first run the unpacked version via JSLint (so that you can find out if there is an error with your unpacked version).

+2
source

Source: https://habr.com/ru/post/1336808/


All Articles