Source "@openzeppelin/contracts/token/ERC721/ERC721.sol" not found: File import callback not supported
Asked Answered
F

8

6

I've imported the Open Zeppelin ERC721 token standard into my VS Code with the Solidity extension, but see the following warnings on all my OZ import statements:

Screenshot of error

Why is this happening and what is the workaround for this warning?

What I've tried:

  • change default workspace compiler to localNodeModule (began to throw other warnings like on the pragma solidity line)

Example of solution I've tried

Farkas answered 26/4, 2022 at 12:18 Comment(0)
W
7

Just install the Solidity+Hardhat Extension ,this will take care of the errror.

Woodring answered 12/5, 2022 at 6:55 Comment(0)
W
4

run below command

npm install @openzeppelin/contracts

Change the import line like this

 import "./node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";        
Wigley answered 10/9, 2022 at 14:12 Comment(0)
D
2

You could try this solution here, the only one that helped me. https://mcmap.net/q/418238/-file-import-callback-not-supported

When you compile programmatically using solc, new syntax was introduced, which you have to include in compile.js.

// New syntax (supported from 0.5.12, mandatory from 0.6.0)
var output = JSON.parse(
solc.compile(JSON.stringify(input), { import: findImports })
);

You should have a helper function for finding imports

function findImports(relativePath) {
//my imported sources are stored under the node_modules folder!
 const absolutePath = path.resolve(__dirname, 'node_modules',   relativePath);
 const source = fs.readFileSync(absolutePath, 'utf8');
 return { contents: source };
}
Daren answered 10/11, 2022 at 7:51 Comment(0)
P
0

Unfortunately I ran into this error too & just gave the path manually:

import "/home/ev1lclow3n/node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";

This solved my error. (I'm a linux user so path may differ for you)

Thanks ;-)

Poetize answered 12/12, 2022 at 21:9 Comment(0)
V
0

you have to manually guide the open zepplin import to its source file if you have it downloaded in your node modules then all you have to do is to change its path like this " ../node_modules/" and also make sure to use the latest extension of juan blanco's solidity extension and solidity and hardhat extension and if you are following a tutorial your first lines of codes would probably be import "hardhat/console.sol"; all you have to do here is to manually direct only this file to its designated place and the others would do it by themselves.

Valeta answered 31/12, 2022 at 7:30 Comment(0)
T
0

What you have to do is:

If you have "Solidity by Juan Blanco" for Truffle and "Solidity by Nomic Foundation" for Hardhdat, and if you are using Hardhat, disable the one by Juan Blanco and just use the one by Nomic Foundation, it just worked for me. Screenshot

Make sure to create a Hardhat project (npx hardhat) and install: npm install --save-dev "hardhat@^2.12.7" "@nomicfoundation/hardhat-toolbox@^2.0.0"

npm i @openzeppelin/contracts

Tarr answered 21/2, 2023 at 20:38 Comment(3)
Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare meta.stackexchange.com/questions/214173/… ). For example like "If your problem is ... then the solution is to .... because .... ."Monomorphic
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Antique
This solution works well. I often switch between Truffle and Hardhat depending on the projects. Recommend to say "If using Truffle only use Solidity by Juan Blanco. If using Hardhat then only use Solidity by Nomic Foundation. (If both are installed then disable one accordingly).Vaish
T
0

you can add following lines at beginning

import "C:\Users\{user_name}\AppData\Roaming\npm";

// so below is my code:
pragma solidity ^0.8.0;

import "C:\Users\Dell\AppData\Roaming\npm"; // you can add your {username} instead of Dell.
import "node_modules/@openzeppelin/contracts/utils/Counters.sol";
import "./node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "./node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
Tarratarradiddle answered 12/3, 2023 at 17:29 Comment(0)
F
-5

Two things you have to do:

(1) Install the OZ library via npm install @openzeppelin/contracts

(2) If you see Error HH606 (i.e. project can't compile), it's likely because The Solidity version pragma statement in these files doesn't match any of the configured compilers in your config.. Ensure that your pragma version matches the version in your hardhat config.

Farkas answered 26/4, 2022 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.