pmk
April 14, 2020, 6:52pm
1
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)
Environment
I’m using:
Truffle 5.0.42
Node 11.2.0
The open zeppelin ERC20 contracts
Details
Token file
1 Like
Hi @pmk ,
Welcome to the community
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
pmk
April 15, 2020, 2:10am
4
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.
1 Like
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.
abcoathup
Split this topic
June 12, 2020, 12:25am
6
A post was split to a new topic: Cannot read property “0” of null
abcoathup
Split this topic
January 5, 2021, 11:35pm
7
jing
January 15, 2021, 2:30am
8
Also make sure you have the repo downloaded on your machine & open zeppelin installed to the directory via:
npm install @openzeppelin /contracts
2 Likes
abcoathup
Split this topic
February 4, 2021, 4:14am
9
Hey, take a look at my solution for it.
Hello there. I was getting the same import error while trying to compile .sol.
I’m using VSCode + solidity plugin.
The problem here was that I had multiple projects on the same workspace. And even when I changed the workspace compiler version to Remote and set the correct version for the project, I got this error:
[Captura de Tela 2021-05-29 às 19.28.42]
The @openzeppelin was download correctly as a dependency in my project /node_modules folder. But the compiler was using the rootWorkspace…