8

Whenever I try to compile my solidity contract, the error ParserError: Source \"@OpenZeppelin/contracts/math/SafeMath.sol\" not found: File import callback not supported gets thrown.

pragma solidity ^0.7.0;

import "@OpenZeppelin/contracts/token/ERC20/ERC20.sol";
import "@OpenZeppelin/contracts/math/SafeMath.sol";

Any ideas what could be causing this?

AlienLobster
  • 111
  • 1
  • 2
  • 5
  • 1
    are you using VSCode plugin to compile it? – Natan Lotério May 29 '21 at 23:05
  • 1
    See my solidity `node_module` referencing here https://stackoverflow.com/a/68459731/8534426 – Asolace Jul 20 '21 at 22:38
  • 1
    Does this answer your question? [File import callback not supported?](https://stackoverflow.com/questions/67321111/file-import-callback-not-supported) – cse Oct 20 '21 at 07:41
  • 1
    If you read the VS Code extension docs (I am using the extension by Juan Blanco), it has a section about openzepplin. You need to edit your settings and add the following (assuming node_modules is in the root dir of your ptoject) : "solidity.packageDefaultDependenciesContractsDirectory": "", "solidity.packageDefaultDependenciesDirectory": "node_modules" – Amarsh Nov 15 '21 at 23:35

9 Answers9

9

Make sure you opened VS Code in the root directory of the project.

In my case, this is how my VS Code directory looked like when I get that same error.

projects
 |___MyContract
   |__contracts
     |__Contract.sol

Reopening VS Code in the MyContract directory fixes it.

MyContract
 |__contracts
   |__Contract.sol
Chan Jing Hong
  • 2,251
  • 4
  • 22
  • 41
8

I have the same problem right now. I'm using truffle+node and I fixed it modifying the import path to a relative path, for example,

pragma solidity ^0.7.0;

import "../node_modules/OpenZeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/OpenZeppelin/contracts/math/SafeMath.sol";

I'm not 100% sure why this happen but I hope I helped you.

Eric
  • 81
  • 1
8

After installing OpenZeppelin close your IDE and re-open it.

Leon Africa
  • 509
  • 6
  • 11
2

For some environments, SafeMath has a different path

import "@openzeppelin/contracts/utils/math/SafeMath.sol";
Radical Ed
  • 178
  • 2
  • 13
1

what worked for me; (I am using Windows10 and visual studio code) -click on the extensions icon -search for the solidity extension -select the uninstall button and click on the drop down arrow -select install another version -select version 0.0.135 -After the installation, click on "Reload required"

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 08 '22 at 13:48
  • I had a similar bug with version 0.0.163 installed and switching to a previous version fixed the issue(I switched to v0.0.153). – Alon Ben Yaakov Jun 12 '23 at 11:03
1

Make sure you installed openzeppelin

npm install --save @openzeppelin/contracts@v3.0.0
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
0

actualy the problem is solidity compiler is stupid and doesn't automatically follow path, you have to specify all allowed path manually (https://docs.soliditylang.org/en/latest/using-the-compiler.html), every each one of them and completely to the file(cannot give juste the higher level folder) use npx hardat compile if you can it's better

Sen'IT
  • 66
  • 2
  • 4
0

This worked for me -

Change the settings in Solidity extension in VSCode.

The default settings include: "solidity.packageDefaultDependenciesContractsDirectory": "contracts" Users need to change the setting from contracts to an empty string. "solidity.packageDefaultDependenciesContractsDirectory": ""

Source - https://github.com/juanfranblanco/vscode-solidity/issues/178

Ved Prakash
  • 101
  • 1
  • 5
0

Try this:

import "{Project Name}/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
Tyler2P
  • 2,324
  • 26
  • 22
  • 31