Published on

Solution for: “ParserError: Source “openzeppelin/contracts/token/*.sol” not found

Problem: Running into the following error message when attempting to run the command truffle migrate:

$ truffle migrate
...
...
ParserError: Source "@openzeppelin/contracts/token/ERC721/ERC721.sol" not found
 --> project:/contracts/ArtToken.sol:3:1:
  |
3 | import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

,ParserError: Source "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol" not found
 --> project:/contracts/ArtToken.sol:4:1:
  |
4 | import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

,ParserError: Source "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol" not found
 --> project:/contracts/ArtToken.sol:5:1:
  |
5 | import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

,ParserError: Source "@openzeppelin/contracts/utils/Counters.sol" not found
 --> project:/contracts/ArtToken.sol:6:1:
  |
6 | import "@openzeppelin/contracts/utils/Counters.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Compilation failed. See above.
Truffle v5.4.26 (core: 5.4.26)
Node v16.13.1

Solution: One of the potential causes for this error could be a missing node_modules folder in the directory which contains the contracts directory. Therefore, truffle cannot locate the @openzeppelin/contracts package.

To solve this, run the npm init command in the directory contains the node_modules folder. Then, run the command npm install --save @openzeppelin/contracts to install the required dependency. Retry the command truffle migrate to see if this resolves the error.

Conclusion

Thanks for reading this blog post!

If you have any questions or concerns please feel free to post a comment in this post and I will get back to you when I find the time.

If you found this article helpful please share it and make sure to follow me on Twitter and GitHub, connect with me on LinkedIn and subscribe to my YouTube channel.