0

I'm following a tutorial on freeCodeCamp and on my contract: I'm getting an error on import "hardhat/console.sol";

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;

import "hardhat/console.sol";

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        console.log("Deploying a Greeter with greeting:", _greeting);
        greeting = _greeting;
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
        greeting = _greeting;
    }
}

Error: Source "hardhat/console.sol" not found: File import callback not supported

When change the path to:

import "../node_modules/hardhat/console.sol";

It works but then I'm unable to compile

However if I go to the file directly like this:

import "C:/Users/limco/Desktop/Dev- Fullstack/react-dapp/node_modules/hardhat/console.sol";

It works and compiles, I don't understand why

1 Answer 1

1

If your project file structure is something like:

  • Project
    • client
    • smart_contracts
      • contracts *contract_A.sol

you'll experience this error especially assuming you're in VSCode cd'ed into the sub directory. You need to open the 'smart_contracts' directory as the root when you execute commands such as npx hardhat test etc. When 'smart_contracts' is the root it will work - though, you might get an error:

opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],

which you can fix by running

export NODE_OPTIONS=--openssl-legacy-provider

Good luck!

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.