微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在安全帽合同中导入 openzeppelin 错误

如何解决在安全帽合同中导入 openzeppelin 错误

我正在尝试使用一些 @openzeppelin/contracts 导入来部署合同。

合同:

pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";

contract EthOrb is ERC721URIStorage,Ownable {
//code
}

Package.json:

{
  "name": "eth-orb-contracts","version": "1.0.0","description": "smart contracts for dapps","main": "hardhat.config.js","scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },"author": "","license": "ISC","devDependencies": {
    "hardhat": "^2.2.0"
  },"dependencies": {
    "@openzeppelin/contracts": "^4.0.0"
  }
}

@openzeppelin/contracts 在我的 node_modules 中,我运行了一个 npm I 再次安装。

预期结果:成功导入 deps。

实际结果: 终端中的错误消息:

Compiling 14 files with 0.8.0
ParserError: Source "node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol" not found: File outside of allowed directories.
 --> contracts/EthOrb.sol:5:1:
  |
5 | import "../node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ParserError: Source "node_modules/@openzeppelin/contracts/utils/Counters.sol" not found: File outside of allowed directories.
 --> contracts/EthOrb.sol:6:1:
  |
6 | import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ParserError: Source "node_modules/@openzeppelin/contracts/access/Ownable.sol" not found: File outside of allowed directories.
 --> contracts/EthOrb.sol:7:1:
  |
7 | import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Error HH600: Compilation Failed

编辑: 删除“../node_modules”也不能解决这个问题。

这会导致 lint 错误

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

错误是:

Compiling 14 files with 0.8.0
ParserError: Source "node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol" not found: File outside of allowed directories.
 --> contracts/EthOrb.sol:5:1:
  |
5 | import "../node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ParserError: Source "node_modules/@openzeppelin/contracts/utils/Counters.sol" not found: File outside of allowed directories.
 --> contracts/EthOrb.sol:6:1:
  |
6 | import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ParserError: Source "node_modules/@openzeppelin/contracts/access/Ownable.sol" not found: File outside of allowed directories.
 --> contracts/EthOrb.sol:7:1:
  |
7 | import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Error HH600: Compilation Failed

解决方法

我刚刚遇到了这个问题,并通过将整个“@openzeppelin/contracts”移动到合同编写位置的根来解决它。

示例: 我有一个 Contracts 文件夹,里面有 @openzeppelin 文件夹和 MyContract.sol 文件。 然后我就这样导入了合约:

导入“./@openzeppelin/contracts/token/ERC20/ERC20.sol”;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?