Solidity – Using solc Command to Compile a Single Solidity File Locally

solidity

I want to find a simple way to compile a single sol file on local, without using truffle, remix etc. Something i'd do for js file like running node example.js in command line.

I installed solc using npm install -g solc. Then I tried to compile my file with solc --bin sourceFile.sol. However, it returns "command not found: solc".

solcjs --version

returns "0.8.9+commit.e5eed63a.Emscripten.clang"

solc --version

return command not found: solc

Question

  1. How do I check solc version I just installed?
  2. I don't want to wrap my sol file in a javascript file and run node index.js. Is there a more direct command that simply run the sol file without working around it, maybe like solc ..... example.sol?

Additionally, i also tried

solcjs --bin example.sol

it got me the following. why is that?

Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: " to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
–> example.sol

Best Answer

The version of Solc node installs is as you found out, solcjs. This is the command you would use to compile your contract (as you attempted).

Where things have gone wrong is the missing SPDX entry that is now required at the top of all smart contracts since Solidity 0.6.8

This should be a simple fix by simply adding the SPDX line to the beginning of your contract before the pragma as follows:

// SPDX-License-Identifier: UNLICENSED

Once you make this change to your smart contract it should compile barring any further issues. You do not need to compile with truffle it just as others have suggested makes the development process a bit more streamlined and provides migration and testing features.

For more information regarding the SPDX please see the information here, SPDX License Identifier

Related Topic