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

I’m tryinng to compile a token contract on truffle, and get the error:

Error: Error: Could not find
import from any sources; imported from C:/Users/Angel/Documents/blockchain course/ethereum game/solidity/contracts/GameToken.sol
at Object.compile (C:\Users\Angel\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\workflow-compile\legacy\index.js:72:1)

:computer: Environment
I’m using:
Truffle 5.0.42
Node 11.2.0
The open zeppelin ERC20 contracts

:memo:Details
Token file

Migration file

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);
    }
}

Awesome! Thanks for the lengthy reply, I will try all that.

EDIT: Yes!!! That was it, update truffle and use the newest version of the contracts.

Hi @pmk,

Glad to be of help. If you get a chance it would be great if you could introduce yourself.

Feel free to ask all the questions that you need.

Also make sure you have the repo downloaded on your machine & open zeppelin installed to the directory via:

npm install @openzeppelin/contracts

Hey, take a look at my solution for it.