solcjs
Compile Solidity smart contracts to bytecode
TLDR
Compile a specific contract to hex
Compile the ABI of a specific contract
Specify a base path to resolve imports from
Specify one or more paths to include containing external code
Optimise the generated bytecode
SYNOPSIS
solcjs [options] file...
solcjs --standard-json
solcjs --help
solcjs --version
PARAMETERS
--version
Displays the current version of the solcjs compiler.
--optimize
Enables the bytecode optimizer, which attempts to reduce transaction costs and contract size.
--bin
Outputs the compiled contract bytecode in hexadecimal format.
--abi
Outputs the Application Binary Interface (ABI) for compiled contracts, essential for contract interaction.
--output-dir
Specifies a directory where all compilation output files should be written.
--base-path
Sets the base path for resolving imports, often used for project root directories.
--include-path
Adds an additional path for resolving imports, useful for library dependencies.
--standard-json
Instructs solcjs to accept a Standard JSON Input object from stdin and print a Standard JSON Output object to stdout. This is the recommended interface for programmatic use.
--pretty-json
Formats JSON output with indentation, making it more human-readable.
--help
Displays a help message with available options and usage information.
DESCRIPTION
solcjs is the command-line interface for the JavaScript implementation of the Solidity compiler. It enables developers to compile Solidity smart contracts directly from the command line, generating bytecode, ABI (Application Binary Interface), and other artifacts necessary for deploying and interacting with contracts on Ethereum-compatible blockchains. Being Node.js-based, solcjs is easily integrated into JavaScript development environments, build scripts, and CI/CD pipelines, offering a flexible alternative or complement to the C++-based solc compiler. It supports various compilation settings, including optimization, import path resolution, and detailed output selection, making it a crucial tool in modern blockchain development workflows.
CAVEATS
solcjs requires Node.js to be installed and available in the system's PATH. Complex projects with multiple source files and intricate import structures often benefit more from using the Standard JSON Input interface rather than direct file paths and `--include-path`/`--base-path` options, as it provides more robust import resolution and error reporting. Performance characteristics might differ slightly from the C++ solc compiler.
STANDARD JSON INPUT/OUTPUT
For advanced and programmatic use, solcjs strongly recommends using the Standard JSON Input format. This allows developers to pass a comprehensive JSON object containing all source files, compiler settings, and output selections, receiving a structured JSON output. This method ensures robust and reproducible compilation across different environments and is the backbone of most high-level Solidity development frameworks.
INTEGRATION WITH DEVELOPMENT FRAMEWORKS
Popular Ethereum development frameworks such as Truffle, Hardhat, and Brownie (via their underlying JS components) frequently utilize solcjs (or the solc-js npm package) behind the scenes for compiling smart contracts. These frameworks abstract away the complexities of direct solcjs command-line invocation, providing a streamlined development experience while leveraging the compiler's capabilities.
HISTORY
solcjs originated as the Node.js binding for the core Solidity compiler, aiming to provide a JavaScript-native interface to the powerful solc compilation engine. Its development was driven by the rapid growth of the Ethereum ecosystem and the increasing adoption of JavaScript/Node.js for decentralized application (dApp) development and blockchain tooling. By offering a familiar environment for compilation, solcjs significantly eased integration into existing web development workflows, build tools, and frameworks like Truffle and Hardhat, making Solidity contract compilation more accessible to a broader developer community primarily working with Node.js.