solc-js

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

JavaScript bindings for the Solidity compiler (solc). Version 0.8.28 provides access to Solidity compilation via Node.js, using Emscripten-compiled binaries from solc-bin. It is released frequently, tracking Solidity releases. Key differentiators: it is the official JS wrapper for the Solidity compiler, supports both high-level (Standard JSON I/O) and low-level APIs, and provides a command-line tool (solcjs) for compilation. Unlike the native binary, it runs entirely in Node.js without additional dependencies.

error Error: CompileError: Source file requires different compiler version (current compiler is 0.8.28+commit.8e4c6f11.Emscripten.clang)
cause The Solidity source specifies a different pragma version than the compiler version installed.
fix
Update the pragma in your .sol file or install a compatible version of solc.
error Error: Cannot find module 'solc'
cause The package is not installed or path is incorrect.
fix
Run 'npm install solc' in your project directory.
error TypeError: solc.compile is not a function
cause Using ESM import ('import solc from 'solc'') instead of CommonJS require or incorrect version.
fix
Use 'const solc = require('solc');' and ensure you have the latest version installed.
breaking Since version 0.6.0, compile() only accepts an object for callbacks, not individual callback functions.
fix Pass an object with import and/or smtSolver properties instead of separate function arguments.
gotcha The command-line tool solcjs is not compatible with solc from the Solidity package; it cannot be used with eth.compile.solidity() RPC method.
fix Use solcjs for standalone compilation; for Ethereum client integration, install the native solc binary.
gotcha Import callback must be synchronous; async file reads will not work.
fix Use synchronous file reads (e.g., fs.readFileSync) or preload all dependencies before compiling.
deprecated compileStandardWrapper is deprecated in favor of compile() with an object callback (since 0.6.0).
fix Use solc.compile(JSON.stringify(input), { import: ... }) instead.
npm install scol
yarn add scol
pnpm add scol

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

const solc = require('solc');
const input = {
  language: 'Solidity',
  sources: {
    'test.sol': {
      content: '// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\ncontract Test { }'
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['evm.bytecode.object']
      }
    }
  }
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output.contracts['test.sol'].Test.evm.bytecode.object);