solc-js
raw JSON → 0.8.35 verified Fri May 01 auth: no javascript
JavaScript bindings for the Solidity compiler (solc), providing a high-level and low-level API to compile Solidity smart contracts to EVM bytecode. The current stable version is 0.8.35, following the Solidity compiler releases closely. It uses Emscripten-compiled Solidity binaries from solc-bin, supporting all compiler features via the Standard JSON interface. Key differentiators: it's the official JavaScript wrapper for Solidity, enabling dynamic compilation in Node.js and browser environments without a native binary. The package ships TypeScript types and supports both CommonJS and ESM modules (ESM requires Node >=12).
Common errors
error Error: No input specified ↓
cause compile() called without arguments or with invalid input.
fix
Ensure input is a non-empty JSON string with 'language', 'sources', and 'settings' fields.
error TypeError: solc.compile is not a function ↓
cause Import style mismatch (e.g., using default import when only named export exists).
fix
Use import { compile } from 'solc' instead of import solc from 'solc'.
error Module not found: Can't resolve 'solc' ↓
cause Package not installed or ESM resolution issue (package not in node_modules).
fix
Run 'npm install solc' and ensure Node.js version >=12 for ESM or use CommonJS require.
error solc.compile is not a function (when using require) ↓
cause Older solc versions (pre-0.8.11) had different export shape.
fix
Use solc.compile() directly (it always was a function) or upgrade to >=0.8.11 and use named export.
Warnings
breaking Breaking change in v0.8.11: compile() now returns JSON string instead of object. Wrap with JSON.parse. ↓
fix Wrap compile() call with JSON.parse() to get the output object.
deprecated Legacy callback-based API removed in v0.8.0. Use compile(input, { import: ... }) object syntax. ↓
fix Update to use the object-based callback parameter: compile(input, { import: myImportFn }).
gotcha The import callback must be synchronous. Async callbacks will cause undefined behavior. ↓
fix Pre-fetch all dependencies synchronously or use a synchronous file reader.
breaking Version 0.6.0 dropped support for Node <6. Node 12+ required for ESM support. ↓
fix Upgrade Node.js to >=10 (preferably >=12 for ESM).
deprecated solcjs CLI is not compatible with native solc CLI. It lacks some features. ↓
fix Use native solc binary for full feature set or rely on solc-js CLI for simple compilation.
Install
npm install solc yarn add solc pnpm add solc Imports
- compile wrong
const solc = require('solc'); solc.compile(...)correctimport { compile } from 'solc' - default wrong
import * as solc from 'solc'correctimport solc from 'solc' - compileCallback
import { compileCallback } from 'solc' - version wrong
const solc = require('solc'); console.log(solc.version)correctimport { version } from 'solc'
Quickstart
import { compile } from 'solc';
const input = {
language: 'Solidity',
sources: {
'test.sol': {
content: 'contract C { function f() public { } }'
}
},
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode.object']
}
}
}
};
const output = JSON.parse(compile(JSON.stringify(input)));
console.log(output.contracts['test.sol'].C.evm.bytecode.object);