JavaScript bindings for the Solidity compiler with Standard JSON I/O interface
—
Command-line interface for Solidity compilation providing standalone compilation capabilities without requiring programmatic API usage.
When installed globally, solc provides the solcjs command-line tool.
# Global installation
npm install -g solc
# Basic usage
solcjs --helpCompile Solidity files to bytecode and ABI.
# Compile to bytecode
solcjs --bin Contract.sol
# Compile to ABI
solcjs --abi Contract.sol
# Both bytecode and ABI
solcjs --bin --abi Contract.solHandle complex project layouts with imports and dependencies.
# Compile with base path and include paths
solcjs --bin --include-path node_modules/ --base-path . MainContract.sol
# Multiple include paths
solcjs --bin --include-path ./lib --include-path ./node_modules Contract.solUse Standard JSON input/output format for advanced compilation.
# Standard JSON mode (reads from stdin)
echo '{"language":"Solidity","sources":{"test.sol":{"content":"contract C {}"}}}' | solcjs --standard-jsonStandard JSON Input Example:
{
"language": "Solidity",
"sources": {
"Contract.sol": {
"content": "contract MyContract { uint public value = 42; }"
}
},
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": ["abi", "evm.bytecode"]
}
}
}
}Control bytecode optimization settings.
# Enable optimizer
solcjs --optimize --bin Contract.sol
# Set optimizer runs
solcjs --optimize --optimize-runs 1000 --bin Contract.solControl output format and destination.
# Specify output directory
solcjs --bin --output-dir ./build Contract.sol
# Pretty-print JSON output
solcjs --abi --pretty-json Contract.sol
# Verbose output
solcjs --bin --verbose Contract.solsolcjs [options] <files...>
Options:
--version Show version and exit
--optimize Enable bytecode optimizer (default: false)
--optimize-runs <runs> Number of optimizer runs (default: 200)
--bin Output binary bytecode
--abi Output ABI specification
--standard-json Use Standard JSON I/O mode
--base-path <path> Root of the project source tree
--include-path <paths...> Extra source directories for imports
-o, --output-dir <dir> Output directory for compiled files
-p, --pretty-json Pretty-print JSON output (default: false)
-v, --verbose Verbose output (default: false)
-h, --help Display help informationThe CLI uses a sophisticated path resolution system:
. for current directory)# Correct usage
solcjs --bin --base-path . --include-path ./node_modules MyContract.sol
# Files outside paths will cause errors
solcjs --bin --base-path ./src ../external/Library.sol # Error!The CLI generates files with specific naming conventions:
# Input: Contract.sol containing contracts A and B
solcjs --bin --abi Contract.sol
# Generated files:
# Contract_sol_A.bin - Bytecode for contract A
# Contract_sol_A.abi - ABI for contract A
# Contract_sol_B.bin - Bytecode for contract B
# Contract_sol_B.abi - ABI for contract BThe CLI provides detailed error reporting:
# Compilation errors
solcjs --bin InvalidContract.sol
# Exit code: 1
# Output includes formatted error messages with line numbers
# Warning handling (warnings don't cause non-zero exit)
solcjs --bin ContractWithWarnings.sol
# Exit code: 0
# Warnings printed to stdoutBuild Script Integration:
#!/bin/bash
set -e # Exit on any error
echo "Compiling contracts..."
solcjs --bin --abi --optimize --output-dir ./build *.sol
echo "Compilation successful!"
ls -la ./build/Package.json Scripts:
{
"scripts": {
"compile": "solcjs --bin --abi --optimize --output-dir ./build contracts/*.sol",
"compile:dev": "solcjs --bin --abi --verbose contracts/*.sol",
"compile:standard": "cat input.json | solcjs --standard-json --pretty-json"
}
}CI/CD Pipeline:
# GitHub Actions example
- name: Compile Solidity contracts
run: |
npm install -g solc
solcjs --bin --abi --optimize \
--base-path . \
--include-path ./node_modules \
--output-dir ./dist \
contracts/*.solThe CLI automatically attempts to use SMT solvers for formal verification:
# Compile with SMT checking (requires Z3, Eldarica, or cvc5)
echo '{
"language": "Solidity",
"sources": {
"test.sol": {
"content": "contract C { function f(uint x) public { assert(x > 0); } }"
}
},
"settings": {
"modelChecker": {
"engine": "chc",
"solvers": ["smtlib2"]
}
}
}' | solcjs --standard-jsonIf SMT solvers are not available:
>>> Cannot retry compilation with SMT because there are no SMT solvers available.Important limitations compared to the native solc binary:
eth.compile.solidity() RPC methodWhen to use solcjs CLI:
When to use native solc:
Install with Tessl CLI
npx tessl i tessl/npm-solc