solc-js

raw JSON →
0.8.1 verified Fri May 01 auth: no javascript

JavaScript bindings for the Solidity compiler, enabling Solidity smart contract compilation directly in Node.js or the browser via Emscripten. This version 0.8.1 is a fixed release of the official npm package by the Ethereum Foundation. It provides both a high-level API (uniform `compile` method using Standard JSON I/O) and a low-level API exposing compiler internals. Key differentiators: it wraps the native Solidity compiler, supports imports via callbacks, and the command-line interface (`solcjs`) is not compatible with the native `solc` binary. Major breaking changes include the removal of the old callback-based API since v0.6.0. Regular releases track Solidity compiler versions.

error Error: Requires at least one source file.
cause solcjs called without specifying a .sol file.
fix
Provide the contract file: solcjs --bin MyContract.sol
error Error: solc: TypeError: Cannot read property 'compile' of undefined
cause Incorrect import: using default import instead of named import in ESM.
fix
Use import { compile } from 'solc'; or const { compile } = require('solc');
error Error: Source file requires different compiler version (current compiler is 0.8.1)
cause Contract pragma does not match installed solc version.
fix
Install the correct solc version matching the pragma, e.g., npm install solc@0.8.1 or pragma solidity ^0.8.0.
breaking The callback-based API (passing a function as second argument to compile) is removed. Use the object-based interface with `import` callback instead.
fix Pass an object with `import` and `smtSolver` keys, not a direct function.
gotcha solcjs command-line tool is incompatible with the native solc binary and cannot be used with eth.compile.solidity().
fix Use the native Solidity compiler for RPC-based compilation or switch to solc-js high-level API.
gotcha The `import` callback must be synchronous. Asynchronous filesystem access will cause issues.
fix Resolve dependencies synchronously or pre-collect them.
deprecated The `smtSolver` callback may not be supported in future versions.
fix Avoid relying on SMT solver integration; use external tools.
npm install solc-0.8.1-fixed
yarn add solc-0.8.1-fixed
pnpm add solc-0.8.1-fixed

Compiles a minimal Solidity contract using the high-level API and prints the bytecode.

const solc = require('solc');
const input = {
  language: 'Solidity',
  sources: {
    'test.sol': { content: 'contract C { function f() public { } }' }
  },
  settings: { outputSelection: { '*': { '*': ['*'] } } }
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
for (const contractName in output.contracts['test.sol']) {
  console.log(`${contractName}: ${output.contracts['test.sol'][contractName].evm.bytecode.object}`);
}