【问题标题】:Source "@openzeppelin/contracts/token/ERC721/ERC721.sol" not found: File import callback not supported未找到源“@openzeppelin/contracts/token/ERC721/ERC721.sol”:不支持文件导入回调
【发布时间】:2022-11-11 20:00:33
【问题描述】:
标签:
visual-studio-code
solidity
openzeppelin
【解决方案1】:
只需安装 Solidity+Hardhat Extension 即可解决错误。
【解决方案2】:
运行以下命令
npm install @openzeppelin/contracts
像这样更改导入行
import "./node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
【解决方案3】:
您可以在这里尝试此解决方案,这是唯一对我有帮助的解决方案。
https://stackoverflow.com/a/72241149/7537543
当您使用 solc 以编程方式编译时,引入了新语法,您必须将其包含在 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 })
);
您应该有一个帮助函数来查找导入
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 };
}
【解决方案4】:
好的。那是一个愚蠢的问题。你必须做的两件事:
(1) 通过安装 OZ 库
npm install @openzeppelin/contracts
(2) 如果您看到错误 HH606(即项目无法编译),可能是因为The Solidity version pragma statement in these files doesn't match any of the configured compilers in your config.。确保您的编译指示版本与您的安全帽配置中的版本相匹配。
希望这可以帮助。