【问题标题】: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
【问题描述】:

我已经使用 Solidity 扩展将 Open Zeppelin ERC721 令牌标准导入到我的 VS 代码中,但是在我的所有 OZ 导入语句中看到以下警告:

Screenshot of error

为什么会发生这种情况以及此警告的解决方法是什么?

我试过的:

  • 将默认工作区编译器更改为 localNodeModule(开始抛出其他警告,例如在 pragma solidity 行上)

Example of solution I've tried

【问题讨论】:

    标签: 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.。确保您的编译指示版本与您的安全帽配置中的版本相匹配。

          希望这可以帮助。

          【讨论】:

            猜你喜欢
            • 2021-10-06
            • 2022-11-27
            • 2021-05-04
            • 2021-10-06
            • 1970-01-01
            • 2021-10-08
            • 1970-01-01
            • 2022-01-02
            • 2022-12-10
            相关资源
            最近更新 更多