'Can't import openzeppelin node_modules: File import callback not supported

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?



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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

Solution 4:[4]

For some environments, SafeMath has a different path

import "@openzeppelin/contracts/utils/math/SafeMath.sol";

Solution 5:[5]

Make sure you installed openzeppelin

npm install --save @openzeppelin/[email protected]

Solution 6:[6]

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

Solution 7:[7]

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"

Solution 8:[8]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chan Jing Hong
Solution 2 Eric
Solution 3 Leon Africa
Solution 4 Radical Ed
Solution 5 Henry Ecker
Solution 6 Sen'IT
Solution 7 Onuche Umar
Solution 8 Ved Prakash