9

I created a new Hardhat project and used the advanced TypeScript example.

I also have a VSCode installation with the Solidity extension running.

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

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;
    }
}

I get the error Source "hardhat/console.sol" not found: File import callback not supported.

The project compiles successfully, so the problem seems to lie with the Solidity extension of VSCode.

When I change the import path to "../node_modules/hardhat/console.sol" the error is gone, but the project doesn't compile anymore.

2
  • i'm new to this and am following a tutorial on freecodecamp, I'm assuming you are too? I'm having the same issues as you had, would you mind helping?
    – Lim Coln
    Nov 1, 2021 at 9:58
  • 1
    Open the hardhat project directly in VSCode. contracts and node_modules can't be in subfolders (from the VSCode view)
    – K..
    Nov 2, 2021 at 13:05

10 Answers 10

9

As explained in the README, you have to explicitly refer to node_modules in the VSCode user settings:

"solidity.packageDefaultDependenciesContractsDirectory": "",
"solidity.packageDefaultDependenciesDirectory": "node_modules"

Pro tip: use my hardhat-template, which combines Hardhat, TypeChain, and other goodies.

3
  • Thanks for the answer, but it didn't work. I also tried "../node_modules" and "contracts" as contract directory, but that error wouldn't go away.
    – K..
    Oct 19, 2021 at 11:25
  • 1
    It doesn't work even when you use my template? Oct 19, 2021 at 12:33
  • 1
    I found the issue. Your template works when I open it as the workspace root, but not if I use the parent directory, which is the case in mono-repos. After I opened my hardhat project directory directly, the error was gone.
    – K..
    Oct 20, 2021 at 14:50
8

try to use this import "../node_modules/hardhat/console.sol";

i think, it even makes more sense in solidity

instead of this import "hardhat/console.sol";
doing this would enable you to open your project as sub-folder in vscode.

3

Disable 'solidity' and install 'Solidity + Hardhat' extension will solve the problem.

enter image description here

1
  • Hello. Can you explain why this is the problem and how it is solved by your solution ? Jul 5, 2022 at 6:43
2

I had a folder named smart_contracts. Inside of it I created a folder named hadhat_typescript_sample and communicated with the latter via smart_contracts/

I fixed that error by opening hardhat_typescript_sample in VsCode. Hopefully, it will help you out:)

0

For me the problem was that the version specified in the hardhat.config.js was different from the one in the solidity file.

Hope it helps!

0

this is latest problem because no one in the internet never notice anytime before. so it could come with any new updating or releasing version of hardhat. using this conclusion downgrade my solidity extension to 0.0.136 was solve my problem.

:) hope they fix this issue in next updateenter image description here

0

The problem is with VS Code Solidity Extension. The video will help you solve it. https://youtu.be/5qTdQNCMwk8

1
  • 2
    Hi AustinOnuora! Welcome to Ethereum Stackexchange! Link only answers are discouraged. It is better to include the main idea in the answer and only use the link to expand the details.
    – Ismael
    Jan 10, 2022 at 20:25
0

For me the issue was solved by downgrading the solidity version. I am using VsCode in windows, I was facing issue in version 0.0.136 and the solidity extension is offered by Juan Blanco.

0

You can use this tutorial https://hardhat.org/guides/vscode-tests.html I succeed to solve this problem.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 19, 2022 at 9:26
0

This happen because the name entry in your package.json is "hardhat". Changing it to something else should fix the issue.

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