Errors when compiling smart contracts with Truffle and solc-js


This post has to do with troubleshooting Truffle and solc-js errors using npm.

  1. The first issue I encountered involved an error regarding an ERC721 function. The error was along the lines of:

I was using openzeppelin to import the code for the ERC721 token. When I tried to compile my smart contract, I kept getting an error about this function:

This function is within the ERC721 token code itself and has to do with enabling cross-communication among contracts.

Turns out, I was having an issue with my Truffle version. When you install Truffle and run truffle version in the console, you should get something similar to:

If you don’t get the version of Truffle, Solidity, Node, and Web3, you may have to uninstall truffle and reinstall it. I went into my computer files and manually uninstalled Truffle. You can find your truffle path by typing which truffle in your console:

You can also try the command:

To install truffle globally, you can run:

Check again to see if typing truffle version in your console gives the Truffle, Solidity, Node, and Web3 version. If that still doesn’t solve the issue, try installing truffle to your local project folder. Make sure your in your project’s root directory then run:

(remember to add node_modules to a .gitignore file so your package_lock.json can be tracked). Again, check if you are given four versions.

If you are, try compiling your contract again and hopefully the original error went away! Before you compile, don’t forget to go into your truffle console by typing:

2. Another issue I had was not having the proper solc-js compiler within my project. This compiler enables solidity to be read properly. The solution? When writing your contract, you can declare a range for the solc-js version:

(PS, SPDX stands for Software Package Data Exchange, and it’s a file format according to the license specified for distribution. The license doesn’t have to be MIT, it can be any license of your choosing, but this specification is now part of writing smart contracts in solidity and has to do with contracts verifying other contracts for importing purposes).

If this doesn’t solve your problem, you can check which truffle version you have in your local project. Typing truffle version in your console should give you the Truffle, Solidity, Node, and Web3 version as shown above. If not, follow the steps above to troubleshoot installing Truffle for your needs.

If you have these four versions and the solidity compiler range set up, but you are still getting an error about compiler versions, you can specify the compiler version in your truffle-config.js file under networks in a compiler object. Make sure the version is specified as a string. For the version of Solidity that I have, my file would look like this:

Best of luck!

Errors when compiling smart contracts with Truffle and solc-js was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.