2

I was writing and testing my smart contracts in Remix where I was importing some contracts from OpenZeppelin, and now I want to move it to my project in VSC. But now my VSC is screaming with an error:

Source "@openzeppelin/contracts/token/ERC20/ERC20.sol" not found: File import callback not supported

I went to documentation, I found that I have to install some of the packages to use OZ with Hard Hat like:

npm install --save-dev @openzeppelin/contracts
npm install --save-dev @openzeppelin/hardhat-upgrades
npm install --save-dev @nomiclabs/hardhat-ethers ethers # peer dependencies

https://docs.openzeppelin.com/upgrades-plugins/1.x/hardhat-upgrades

But it still not working it cannot find this contract even if I can see it installed in node_modules.

Here is my package.json dependencies:

{
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.4",
    "@nomiclabs/hardhat-waffle": "^2.0.1",
    "@openzeppelin/contracts": "^4.4.2",
    "@openzeppelin/hardhat-upgrades": "^1.13.0",
    "chai": "^4.3.4",
    "ethereum-waffle": "^3.4.0",
    "ethers": "^5.5.3",
    "hardhat": "^2.8.0"
  }, 
  "dependencies": {}
}

Here is hardhat.config.js:

require('@nomiclabs/hardhat-waffle');
require("@nomiclabs/hardhat-ethers");
require('@openzeppelin/hardhat-upgrades');

module.exports = {
  solidity: '0.8.0',
  networks: {
    ropsten: {
      url: 'https://eth-ropsten.alchemyapi.io/v2/cds................Fa',
      accounts: ['private key']
    }
  }
}

I'm pretty new to Hard Hat, this is my first project.

This is an error I have: enter image description here


EDIT


I fixed this issue by just change the import path to:

import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";

But then when I was running some tests with npx hardhat test I had compilation fail:

enter image description here

So I moved back to the normalimport "@openzeppelin/.../ERC20.sol"; and it compiled successfully, yet my VSC still screaming about that.

So HardHat can read those contract properly, there's just some problem with VSC that it cannot read those paths correctly. Someone know how to fix this?

1
  • Please post the answer as an answer so when others see the question its clear there is a solution
    – Julissa DC
    Jan 31, 2022 at 16:04

4 Answers 4

3

I fixed this issue by just change the import path to:

import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";

But then when I was running some tests with npx hardhat test I had compilation fail:

enter image description here

So I moved back to the normalimport "@openzeppelin/.../ERC20.sol"; and it compiled successfully, yet my VSC still screaming about that.

So HardHat can read those contract properly, there's just some problem with VSC that it cannot read those paths correctly.

1

I used this command import '../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol'; Now it's working.

1
  • 1
    Please provide explanation on why this works. Jun 9, 2023 at 8:28
0

Use single quotes instead of doubles.

Instead of:

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

use:

import '@openzeppelin/contracts/token/ERC20/ERC20.sol';

-1

There is a little bit difference between importing the code in solidity and hardhat code

Solidity code

import "@openzeppelin/contracts/ownership/Ownable.sol";

hardhat code

import "@openzeppelin/contracts/access/Ownable.sol";

Change the path according to your requirement. You can see the contracts path in artificats/openzepeppelin/ contracts. So import accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.