Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 463
AlienLobster Avatar asked Jan 30 '21 15:01

AlienLobster


People also ask

How do I import OpenZeppelin contracts in Vscode?

Just change the import to @openzeppelin/contracts/access/Ownable. sol and it will work in both Remix and vscode-solidity (with vscode extension you of course have to install the package using npm).

What is OpenZeppelin?

OpenZeppelin is an open-source framework to build secure smart contracts. OpenZeppelin provides a complete suite of security products and audit services to build, manage, and inspect all aspects of software development and operations for decentralized applications.

How do I import contracts in solidity?

The import statement in Solidity is pretty simple, just point to the source file of the contract you want to import. // File: Contract_A. sol pragma solidity ^0.8. 0; import './Contract_B.


Video Answer


5 Answers

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
like image 158
Chan Jing Hong Avatar answered Oct 12 '22 07:10

Chan Jing Hong


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.

like image 26
Eric Avatar answered Oct 12 '22 07:10

Eric


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

like image 20
Leon Africa Avatar answered Oct 12 '22 07:10

Leon Africa


For some environments, SafeMath has a different path

import "@openzeppelin/contracts/utils/math/SafeMath.sol";
like image 2
Radical Ed Avatar answered Oct 12 '22 06:10

Radical Ed


Make sure you installed openzeppelin

npm install --save @openzeppelin/[email protected]
like image 1
surepalli mahesh Avatar answered Oct 12 '22 06:10

surepalli mahesh