How to resolve error – DeclarationError: Identifier already declared

You might encounter this error “DeclarationError: Identifier already declared” while compiling Solidity code. The error may be caused by the collision between the use of imported files such as in this case between ERC1155Contract.sol and Authorizable.sol, where ERC155Contract.sol is importing Authorizable.sol as shown below.


import "./Authorizable.sol";
import "@openzeppelin/[email protected]/token/ERC1155/ERC1155.sol";
import "@openzeppelin/[email protected]/access/Ownable.sol";
import "@openzeppelin/[email protected]/security/Pausable.sol";
import "@openzeppelin/[email protected]/token/ERC1155/extensions/ERC1155Burnable.sol";
import "@openzeppelin/[email protected]/token/ERC1155/extensions/ERC1155Supply.sol";
import "@openzeppelin/[email protected]/token/ERC1155/extensions/ERC1155URIStorage.sol";

Inside Authorizable.sol, we initially have import "@openzeppelin/contracts/access/Ownable.sol";

While in ERC1155Contract.sol, we have import "@openzeppelin/[email protected]/access/Ownable.sol";

Thus, this caused the collision where we are importing two identical files from different sources. The issue was resolved by changing import "@openzeppelin/contracts/access/Ownable.sol"; to import "@openzeppelin/[email protected]/access/Ownable.sol"; in Authorizable.sol to match the imported file used in ERC1155Contract.sol