JavaScript bindings for the Solidity compiler with Standard JSON I/O interface
npx @tessl/cli install tessl/npm-solc@0.8.0Solc provides JavaScript/TypeScript bindings for the Solidity compiler, enabling developers to compile Solidity smart contracts directly from Node.js applications and web browsers. It wraps the Emscripten-compiled Solidity compiler and offers both high-level and low-level APIs for compilation using the Standard JSON input/output format.
npm install solcimport solc from "solc";For CommonJS:
const solc = require("solc");For specific utilities:
import linker from "solc/linker";
import abi from "solc/abi";
import translate from "solc/translate";
import smtchecker from "solc/smtchecker";
import smtsolver from "solc/smtsolver";import solc from "solc";
// Standard JSON compilation
const input = {
language: 'Solidity',
sources: {
'Contract.sol': {
content: 'contract MyContract { function get() public pure returns (uint) { return 42; } }'
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
// Access compiled contract
console.log(output.contracts['Contract.sol']['MyContract'].evm.bytecode.object);Solc is built around several key components:
Primary compilation interface supporting Standard JSON I/O format with import callbacks and SMT solver integration. Provides both high-level unified API and low-level version-specific access.
function compile(input: string, callbacks?: CompilationCallbacks): string;
interface CompilationCallbacks {
import?: (path: string) => { contents: string } | { error: string };
smtSolver?: (query: string) => { contents: string } | { error: string };
}Load specific compiler versions remotely and manage compiler instances with feature detection capabilities.
function loadRemoteVersion(
versionString: string,
callback: (error: Error | null, solc?: any) => void
): void;
interface CompilerFeatures {
legacySingleInput: boolean;
multipleInputs: boolean;
importCallback: boolean;
nativeStandardJSON: boolean;
}Link library addresses into compiled bytecode and analyze library dependencies with support for both legacy and modern placeholder formats.
function linkBytecode(bytecode: string, libraries: LibraryAddresses): string;
function findLinkReferences(bytecode: string): LinkReferences;
interface LibraryAddresses {
[qualifiedNameOrSourceUnit: string]: string | { [unqualifiedLibraryName: string]: string };
}Translate and update ABI definitions across different Solidity compiler versions to ensure compatibility.
function update(compilerVersion: string, abi: any[]): any[];Integration with external SMT solvers for formal verification through Solidity's SMTChecker with support for Z3, Eldarica, and cvc5.
function smtCallback(
solverFunction: (query: string, solver?: any) => string,
solver?: any
): (query: string) => { contents: string } | { error: string };
const availableSolvers: Array<{
name: string;
command: string;
params: string;
}>;Legacy output translation and assembly formatting utilities for compatibility with older compiler versions and tooling.
function translateJsonCompilerOutput(output: any, libraries?: any): any | null;
function prettyPrintLegacyAssemblyJSON(assembly: any, source?: string): string;
function versionToSemver(version: string): string;Command-line tool (solcjs) for standalone Solidity compilation with project structure support and Standard JSON I/O.
solcjs [options] <files...>
Options:
--bin Output binary bytecode
--abi Output ABI specification
--standard-json Use Standard JSON I/O mode
--optimize Enable bytecode optimizer
--base-path <path> Root of project source tree
--include-path <paths...> Extra source directories
-o, --output-dir <dir> Output directoryinterface LibraryAddresses {
[qualifiedNameOrSourceUnit: string]: string | { [unqualifiedLibraryName: string]: string };
}
interface LinkReferences {
[libraryLabel: string]: Array<{ start: number, length: number }>;
}
interface CompilerInstance {
version(): string;
semver(): string;
license(): string;
compile(input: string, callbacks?: CompilationCallbacks): string;
loadRemoteVersion(version: string, callback: (err: Error | null, solc?: any) => void): void;
features: CompilerFeatures;
lowlevel: {
compileSingle: ((input: string, optimize: boolean) => string) | null;
compileMulti: ((input: string, optimize: boolean) => string) | null;
compileCallback: ((input: string, optimize: boolean, callback: any) => string) | null;
compileStandard: ((input: string, callback?: any) => string) | null;
};
}