YourNewEmpire
YourNewEmpire

Reputation: 153

Importing openzeppelin in hardhat contracts error

I am attempting to deploy a contract with some @openzeppelin/contracts imports.

The 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
}

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"
  }
}

The @openzeppelin/contracts is in my node_modules and I ran an npm I to install again.

expected outcome: imports deps successfully.

actual outcome: error msg in terminal:

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

Edit: Removing the '../node_modules' doesnt solve this either.

This gives lint errors:

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

And the error is:

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

Upvotes: 4

Views: 8704

Answers (4)

Rohit Goyal
Rohit Goyal

Reputation: 89

There is a little bit difference between importing the code in solidity and hardhat code

Solidity code

import "@openzeppelin/contracts/ownership/Ownable.sol";

hardhat code

import "@openzeppelin/contracts/access/Ownable.sol";

Change the path according to your requirement. You can see the contracts path in artificats/openzepeppelin/ contracts. So import accordingly.

Upvotes: 1

Programmer End
Programmer End

Reputation: 11

open node_modules dir in your project folder and search for "@openzepplin" under that find "contracts" if not found run this command

npm install @openzeppelin/contracts

then you will see it resolved and not showing an error.

Upvotes: 0

Dimfred
Dimfred

Reputation: 312

Following my comment above: I found a slightly better fix, you can create a symlink in your root directory where root is:

root
   contracts
   tests
   artifacts 
   ...
   ln -s node_modules/@openzeppelin

if you create there a symlink openzeppelin will get updated and also you can now access without prepending it as a relative directory, which I like more. So you can now go with import '@openzeppelin/...', hope this helps someone.

Upvotes: -1

Lepar
Lepar

Reputation: 19

I just had this problem and solved it by moving the entire "@openzeppelin/contracts" to the root of where the contract is written.

Example: I have a folder Contracts and inside of it I have the @openzeppelin folder and the MyContract.sol file. Then I just imported the contract like this:

import "./@openzeppelin/contracts/token/ERC20/ERC20.sol";

Upvotes: 1

Related Questions