19

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.

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
Stefanos Chrs
  • 2,228
  • 3
  • 19
  • 46
  • Please point us to the file where this error is occurring. – Bergi Jul 04 '14 at 11:50
  • Have a look at [Javascript function cannot be found](http://stackoverflow.com/q/15573202/1048572). Btw, this is not invalid only in strict mode. – Bergi Jul 04 '14 at 11:54
  • @Bergi it points to the minified JS file from all the angularjs scripts. – Stefanos Chrs Jul 04 '14 at 12:10
  • Try to disable minification, or put each of the files in the project through a linter. – Bergi Jul 04 '14 at 12:16
  • The thing is I need it for production mode, since in the Development mode where all the files are separated and unminified works – Stefanos Chrs Jul 04 '14 at 12:30
  • You say the minifier produces invalid code? Or is it just that your module loader is lazy and that separate file just was not loaded? But it looks as I'd need to crawl through the minified code… – Bergi Jul 04 '14 at 12:58
  • Yes I believe the minifier has something to do about it. Here is the min https://dl.dropboxusercontent.com/u/74461514/mini.scripts.js, Thanks for the trouble helping me solve this out – Stefanos Chrs Jul 04 '14 at 13:09
  • 1
    Congrats, you found a bug somewhere! (Hint: it's not your fault) https://github.com/components/jquery/issues/46 – Bergi Jul 04 '14 at 14:04

5 Answers5

20

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!
        }
    }
}
georg
  • 211,518
  • 52
  • 313
  • 390
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • the only file I found through the scripts that had a function not immediately within another function was app/scripts/modelsawesome.js that had an var declaration above the function, but I moved it below and still nothing – Stefanos Chrs Jul 04 '14 at 12:18
  • 3
    It's about *nesting level*, not about the top of the function code. A variable declaration does not open a block. Still, could you link that file maybe so that I can double-check? – Bergi Jul 04 '14 at 12:20
  • Bergi is right, any block scope that use curly braces: try-catch-finally, if-else if-else, for, switch, and ES6 also define new feature that utilize block scope such as class. you can't: "use strict" { Function bar(){/****/} } This as a whole will throw an error. – L2L2L Oct 09 '14 at 07:56
9

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) {}; 
Liam
  • 27,717
  • 28
  • 128
  • 190
Loay
  • 226
  • 2
  • 8
  • Interestingly I've had a function inside an if statement in a Cordova app without any issues as long as it was above version 4.4.4. Got caught out when an older tablet was used! NOTE: even with use strict – Anthony Joanes Jan 23 '17 at 12:52
7

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"
Stefanos Chrs
  • 2,228
  • 3
  • 19
  • 46
  • 1
    This is the correct answer as function formatting is not a good idea in case of External libs used in the app. – CandleCoder Apr 06 '20 at 13:33
1

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.

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
0

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*/},
    };
}
MAYOBYO HASSAN
  • 467
  • 6
  • 10