Last updated: Feb 29, 2024
Reading time·2 min
To solve the "Duplicate identifier error":
skipLibCheck
option to true
in your tsconfig.json
file.npm i -D @types/node@latest
.Sometimes the error is caused when we use a name that clashes with a globally defined object, a global interface or a member with the same name in a different file (if not using ES modules).
// ⛔️ Duplicate identifier 'Employee'.ts(2300) class Employee {} // ⛔️ Duplicate identifier 'Node'.ts(2300) class Node {} // 👈️ name of global interface // ⛔️ Duplicate identifier 'Array'.ts(2300) class Array {} // 👈️ name of global interface
The Employee
class causes the error because this file is not an ES module. A
module is a file that contains at least 1 import
or export
statement.
import
or export
statement, it is considered a global legacy script which is a common cause of the error.export {}
statementIf you don't have anything to export, simply export {}
.
// ✅ It's an ES module now class Employee {} export {};
We used the export {}
line in our file to mark it as an ES module.
Make sure you haven't used any names of global interfaces or objects like
Node
, Array
, String
in your code.
Another common cause of the error is that your project is pulling in typings from multiple versions of a package.
Set the skipLibCheck
option to true
in your
tsconfig.json.
{ "compilerOptions": { "skipLibCheck": true, // ... rest }, "include": ["src/**/*"], "exclude": ["node_modules"] }
The skipLibCheck option instructs the compiler to skip type-checking for declaration files.
This is needed because sometimes two libraries define two copies of the same type.
Make sure to add node_modules
to your exclude
array (like in the code sample
above) if you haven't.
If your error message shows the name of a typings package, e.g.
/node_modules/typescript/lib...
.
You have to update the version of the types package. For example, this is how
you would update the version of @types/node
.
npm i -D @types/node@latest
Make sure to install the latest version of the @types
package because you
might have an older version (or multiple versions).
If you have the @types/node
package in your dependencies, definitely run
npm i -D @types/node@latest
as it often causes the error.
If the error is not resolved, try to delete your node_modules
and
package-lock.json files, re-run
npm install
and restart your IDE.
# 👇️ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # 👇️ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # 👇️ clean npm cache npm cache clean --force npm install
Restart your IDE and your development server if the error persists.