Browser: Identifier X has already been declared

Viewed 167858

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 ?

4 Answers

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.

For those using next.js and having issues with a js Script file after a route change.

I'm using a custom-file.js file in next.js and to resolve SyntaxError: Identifier 'x' has already been declared error, simply change your const and let variables to var in your custom-file.js.

If your <Script src="custom-file.js" /> in next.js loads but doesn't fire after page navigation.

Solution:

You should force the file to restart after a page route.

A simple trick from next.js Script is to add a query string at the end of your URL. This will force Next.js to send a new request, forcing the file to restart.

Here is an example:

<Script async src={`/assets/js/custom-file.js?v=${something-dynamic}`} />

This was my fix.

Change your const & let variables to var instead and make your Script src URL dynamic

hope it helps you too..

Related