我正在尝试部署带有一些 @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 来再次安装。
预期结果:导入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”;
您不能像在 Solidity 中导入那样在 Hardhat 中导入。您必须指定从 openzeppelin 从位于 artifacts 文件夹中的 openzeppelin 文件夹导入的每个合约的路径。
可靠性代码
import "@openzeppelin/contracts/ownership/Ownable.sol";
安全帽代码
import "../artifacts/@openzeppelin/contracts/access/Ownable.sol";
根据你的目录更改路径
根据我上面的评论: 我找到了一个稍微好一点的修复方法,您可以在 root 所在的根目录中创建一个符号链接:
root
contracts
tests
artifacts
...
ln -s node_modules/@openzeppelin
如果您在那里创建一个符号链接 openzeppelin 将得到更新,并且您现在可以访问而无需将其作为相对目录,我更喜欢这一点。 所以你现在可以选择
import '@openzeppelin/...'
,希望这对某人有帮助。
在项目文件夹中打开node_modules目录,并在下面搜索“@openzepplin”,找到“contracts”,如果找不到,请运行此命令
npm 安装@openzeppelin/contracts
然后您将看到它已解决并且没有显示错误。