4

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
YourNewEmpire
  • 153
  • 3
  • 9

3 Answers3

1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Lepar
  • 19
  • 2
  • This is a solution, but creating symlinks seems like extra steps, is there really no other way? – Dimfred Dec 11 '21 at 22:56
  • You do not create symlinks, you copy the folder of the contracts from the node_modules folder (they are files) to the root folder of the project. – Lepar Dec 13 '21 at 09:03
  • Yeah but then `npm update` are no more managed, which I don't like. In the end copying and symlinking produces the same result. – Dimfred Dec 13 '21 at 14:08
  • That's true, this is just a temporary workaround that I found, hopefully they'll fix this in the future to keep it updated – Lepar Dec 13 '21 at 22:31
  • Yes no thats an okay workaround just wondered whether someone knows something new or something. I tried to find a config variable to specify the `include_directories` where the compiler should look for `*.sol`, but haven't found anything. That would probably be a clean fix. VSCode does it in that way for example. – Dimfred Dec 14 '21 at 18:11
-1

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.

Dimfred
  • 182
  • 2
  • 16
-1

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.