gpt4 book ai didi

ethereum - 在安全帽契约(Contract)中导入 openzeppelin 错误

转载 作者:行者123 更新时间:2023-12-04 03:34:05 27 4
gpt4 key购买 nike

我正在尝试使用一些 @openzeppelin/contracts 导入来部署契约(Contract)。
契约(Contract):

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
}
包.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 再次安装。
预期结果:导入成功。
实际结果:
终端中的错误消息:
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”移到编写契约(Contract)的根目录来解决它。
例子:
我有一个 Contracts 文件夹,里面有 @openzeppelin 文件夹和 MyContract.sol 文件。
然后我就这样导入了契约(Contract):
导入“./@openzeppelin/contracts/token/ERC20/ERC20.sol”;

关于ethereum - 在安全帽契约(Contract)中导入 openzeppelin 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67285242/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com