[Ethereum] Importing .sol files from an node_modules folder

solcsolidity

Did i miss something in configuration, or will this be fixed in an future version?

Best regards

# ls -al
Makefile
README.md
build
cmd
contracts
node_modules
package.json

# ls -al ./contracts
Wallet.sol
cat  ./contracts/Wallet.sol
pragma solidity ^0.5.0;

import "openzeppelin-eth/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-eth/contracts/token/ERC20/StandaloneERC20.sol";
import "openzeppelin-eth/contracts/ownership/Ownable.sol";
import "zos-lib/contracts/Initializable.sol";

contract Wallet is Ownable {
    function transferERC20(IERC20 token, address to, uint256 amount) public onlyOwner returns (bool) {
        require(token.transfer(to, amount));
    }
}
# solc --abi --bin ./contracts/Wallet.sol -o ./build
./contracts/Wallet.sol:3:1: Error: Source "openzeppelin-eth/contracts/token/ERC20/IERC20.sol" not found: File outside of allowed directories.
import "openzeppelin-eth/contracts/token/ERC20/IERC20.sol";
^---------------------------------------------------------^
./contracts/Wallet.sol:4:1: Error: Source "openzeppelin-eth/contracts/token/ERC20/StandaloneERC20.sol" not found: File outside of allowed directories.
import "openzeppelin-eth/contracts/token/ERC20/StandaloneERC20.sol";
^------------------------------------------------------------------^
./contracts/Wallet.sol:5:1: Error: Source "openzeppelin-eth/contracts/ownership/Ownable.sol" not found: File outside of allowed directories.
import "openzeppelin-eth/contracts/ownership/Ownable.sol";
^--------------------------------------------------------^
./contracts/Wallet.sol:6:1: Error: Source "zos-lib/contracts/Initializable.sol" not found: File outside of allowed directories.
import "zos-lib/contracts/Initializable.sol";

Best Answer

The reason for this error is that truffle compile does some path remapping magic to find files which are imported from the node_modules folder.

If you are using the command line compiler version of solc, you need to provide it these path remappings yourself.

The following command should work if it is run from the same directory as the node_modules dir:

solc openzeppelin-solidity/=$(pwd)/node_modules/openzeppelin-solidity/ contracts/**/*.sol

Notes:

  1. The path mapped to must be an absolute path, so using $(pwd) in the example above enable that without having to type the full path.
  2. I don't know how to make this generic to any directory within node_modules you need to do this for each npm installed project you import solidity files from.
  3. If you're getting File outside of allowed directories errors, you can add the --allow-paths $(pwd).

See the docs for more info.

Related Topic