solc-js

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

JavaScript bindings for the Solidity compiler, wrapping Emscripten-compiled binaries from solc-bin. The current stable version is 0.8.28 (as of 2025), with a release cadence following Solidity releases (monthly). Key differentiators: it is the official JS wrapper for the Solidity compiler, supports standard JSON I/O (compileStandard/compileStandardWrapper), legacy compile() API, and import callbacks for resolving dependencies. Alternative is solc-typed by TypeChain but this is the canonical package for compiling Solidity in Node.js and browser environments.

error Error: require() of ES Module /path/to/solc/index.js from /path/to/app.js not supported.
cause Using import solc from 'solc' in a CJS context, or using newer solc versions that are ESM-only.
fix
Use const solc = require('solc') for CJS, or update to bundler that supports ESM.
error TypeError: solc.compile is not a function
cause Attempting to call compile on newer solc versions where compile is not exported (>=0.6.0).
fix
Use solc.compileStandardWrapper instead.
error TypeError: Cannot read properties of undefined (reading 'bytecode')
cause Assuming output.contracts always exists; but if compilation fails, output may have errors instead.
fix
Check output.errors before accessing contracts.
breaking In solc >=0.6.0, the old compile() API with object sources (e.g., solc.compile({sources:...}, optimizer, findImports)) is removed; use compileStandard or compileStandardWrapper instead.
fix Use compileStandardWrapper with JSON string input as shown in quickstart.
deprecated The method compile() is deprecated in favor of compileStandardWrapper since version 0.4.11 and will be removed.
fix Switch to compileStandardWrapper.
gotcha solc.compile() returns an object with 'errors' array of strings, but compileStandardWrapper returns a JSON string. Parsing the JSON string may throw if errors are present.
fix Always wrap compileStandardWrapper output in try/catch or check for errors before parsing.
npm install krzkaczor-solc
yarn add krzkaczor-solc
pnpm add krzkaczor-solc

Compiles a Solidity contract using the standard JSON input/output API (compile wrapper with JSON string).

const solc = require('solc');
const input = {
  language: 'Solidity',
  sources: {
    'test.sol': {
      content: 'contract C { function f() public pure returns (uint) { return 1; } }'
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
if (output.errors) {
  output.errors.forEach(e => console.error(e.formattedMessage));
} else {
  console.log(output.contracts['test.sol'].C.abi);
}