Stefanos Chrs
Stefanos Chrs

Reputation: 2518

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.

Upvotes: 19

Views: 43540

Answers (5)

MAYOBYO HASSAN
MAYOBYO HASSAN

Reputation: 506

Interestingly, mine was because the { was put on a new line, yet i was returning an object so i changed

from

"use strict";
var funcName1 = function(){
    /* some code*/
    return
    { // note this bracket
        funcName2:function(){/* some code*/},
    };
}

to

"use strict";
var funcName1 = function(){
    /* some code*/
    return { // to this, same line
        funcName2:function(){/* some code*/},
    };
}

Upvotes: 0

Ole Albers
Ole Albers

Reputation: 9295

In addition to the correct answers, this could also be a bug in FireFox in some specific scenarios.

We had this error message on the machine of one single user. In the JavaScript file there was a use strict line below the method that throwed this error (which should not be affected by this)

It happened to be an issue on FireFox Version 45.9.0 (and maybe older versions, as well). Updating Firefox to the most current version (currently 52.4) solved the issue.

Upvotes: 1

Loay
Loay

Reputation: 226

As someone suggested above, you can uncomment the 'use strict'; part, or even better, change your function syntax

instead of

function funcName (param) { }

use

funcName = function(param) {}; 

Upvotes: 9

Bergi
Bergi

Reputation: 664599

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

Upvotes: 20

Stefanos Chrs
Stefanos Chrs

Reputation: 2518

The way I solved the problem was by removing the 'use strict' that was above the jquery in the final minified script. Another way can be changing the jQuery version to one without the strict bug

EDIT: After all it was a jQuery minification error on version 1.11, and an easy fix for this is to go to your Grunt file and comment out the line

banner: "'use strict';\n"

Upvotes: 7

Related Questions