Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser: Identifier X has already been declared

I am using ES6 with Babel in my project and I am getting an error when I declare one of my const

'use strict';

const APP = window.APP = window.APP || {};
const _ = window._;

APP.personalCard = (function () {

   ...

}());

the error

Uncaught TypeError: Identifier 'APP' has already been declared

and that is the whole file, I don't have that declare anywhere else in that file. But I have declared that var in the top of the other files.

What do you think it should be ?

like image 348
Non Avatar asked Mar 21 '16 19:03

Non


3 Answers

But I have declared that var in the top of the other files.

That's the problem. After all, this makes multiple declarations for the same name in the same (global) scope - which will throw an error with const.

Instead, use var, use only one declaration in your main file, or only assign to window.APP exclusively.
Or use ES6 modules right away, and let your module bundler/loader deal with exposing them as expected.

like image 186
Bergi Avatar answered Oct 21 '22 23:10

Bergi


I had a very close issue but in my case, it was Identifier 'e' has already been declared.

In my case caused because of using try {} catch (e) { var e = ... } where letter e is generated via minifier (uglifier).

So better solution could be use catch(ex){} (ex as an Exception)

Hope somebody who searched with the similar question could find this question helpful.

like image 5
sambua Avatar answered Oct 21 '22 22:10

sambua


Remember that window is the global namespace. These two lines attempt to declare the same variable:

window.APP = { ... }
const APP = window.APP

The second definition is not allowed in strict mode (enabled with 'use strict' at the top of your file).

To fix the problem, simply remove the const APP = declaration. The variable will still be accessible, as it belongs to the global namespace.

like image 3
slezica Avatar answered Oct 22 '22 00:10

slezica