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

Stefanos Chrs picture Stefanos Chrs · Jul 4, 2014 · Viewed 34k times · Source

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.

Answer

Bergi picture Bergi · Jul 4, 2014

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!
        }
    }
}