Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function

Hello when I run this project in Developer mode (grunt server) https://github.com/kennethlynne/generator-angular-xl everything is ok but when I run it in production mode (grunt build) I get an `

Uncaught SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function

Anyone have any idea what's going on? Thanks,

Ps. I posted a link to the project instead of code since the JS is split in many files.

like image 296
Stefanos Chrs Avatar asked Jul 04 '14 11:07

Stefanos Chrs


People also ask

How do you declare strict mode?

Strict mode is declared by adding "use strict"; to the beginning of a script or a function.

What is correct way to run a JavaScript in strict mode?

Using Strict mode for a function: Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or 'use strict';) in the function's body before any other statements. Examples of using Strict mode: Example: In normal JavaScript, mistyping a variable name creates a new global variable.

Do JavaScript modules automatically use strict mode?

If you are using native JavaScript modules, then all the code within your modules will be in strict mode by default.


1 Answers

It's just what the error message says:

functions can only be declared at top level or immediately within another function

You must not put a function declaration inside any other block, like an if-statement or for-loop.

Example:

'use strict';

function some() {

    function okay() {
    }

    let x = 1;

    function no_problem() {
    }

    if (x == 1) {

        function BOOM() {   // <- wrong!
        }
    }
}
like image 104
Bergi Avatar answered Oct 01 '22 12:10

Bergi