Import OpenZeppelin Contracts on Windows: "Could not find import from any sources" error

Hi @pmk,

Welcome to the community :wave:

I’m sorry that you are having an issue importing.

I can compile an example GLDToken.sol (smart contract below) using Truffle.

$ npx truffle compile

Compiling your contracts...
===========================
> Compiling ./contracts/GLDToken.sol
> Compiling ./contracts/Migrations.sol
> Compiling @openzeppelin/contracts/GSN/Context.sol
> Compiling @openzeppelin/contracts/math/SafeMath.sol
> Compiling @openzeppelin/contracts/token/ERC20/ERC20.sol
> Compiling @openzeppelin/contracts/token/ERC20/ERC20Detailed.sol
> Compiling @openzeppelin/contracts/token/ERC20/IERC20.sol
> Artifacts written to /home/abcoathup/projects/forum/pmk/build/contracts
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang

I am using Truffle 5.1.21 and OpenZeppelin Contracts 2.5.0 on WSL2. (Windows Subsystem for Linux)

$ npx truffle version
Truffle v5.1.21 (core: 5.1.21)
Solidity v0.5.16 (solc-js)
Node v10.19.0
Web3.js v1.2.1

In your code you are using the old package name openzeppelin-solidity which is now @openzeppelin/contracts

I suggest that you install the latest version of OpenZeppelin Contracts 2.5 (see documentation for instructions: https://docs.openzeppelin.com/contracts/2.x/#overview). You may also need to install the latest version of Truffle. (I have truffle installed locally rather than globally).

As an aside, I assume that you are using Windows. You may want to look at installing WSL2 so that you can run tools on Linux. (How to Install WSL for Windows 10 - Includes WSL2 Optional Steps)

GLDToken.sol

Code from OpenZeppelin Contracts documentation: https://docs.openzeppelin.com/contracts/2.x/erc20#constructing-an-erc20-token-contract

pragma solidity ^0.5.0;

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

contract GLDToken is ERC20, ERC20Detailed {
    constructor(uint256 initialSupply) ERC20Detailed("Gold", "GLD", 18) public {
        _mint(msg.sender, initialSupply);
    }
}
3 Likes