JavaScript bindings for the Solidity compiler with Standard JSON I/O interface
—
Load specific compiler versions remotely and manage compiler instances with feature detection capabilities.
Load specific Solidity compiler versions from the official solc-bin repository on GitHub.
/**
* Load a specific compiler version from GitHub
* @param versionString - Full version string with commit hash (e.g., "v0.8.17+commit.8df45f5f")
* @param callback - Function called with error or compiler instance
*/
function loadRemoteVersion(
versionString: string,
callback: (error: Error | null, solc?: CompilerInstance) => void
): void;Usage Examples:
import solc from "solc";
// Load specific version
solc.loadRemoteVersion('v0.8.17+commit.8df45f5f', (err, solcSnapshot) => {
if (err) {
console.error('Failed to load compiler:', err);
return;
}
// Use the loaded compiler instance
const input = {
language: 'Solidity',
sources: {
'test.sol': {
content: 'contract Test { uint public value = 42; }'
}
},
settings: {
outputSelection: { '*': { '*': ['*'] } }
}
};
const output = solcSnapshot.compile(JSON.stringify(input));
console.log('Compiled with version:', solcSnapshot.version());
});
// Load latest development snapshot
solc.loadRemoteVersion('latest', (err, solcSnapshot) => {
if (err) {
console.error('Failed to load latest:', err);
return;
}
console.log('Loaded latest version:', solcSnapshot.version());
});The version string must be in the long format with commit hash. You can find available versions at the publicly available release list.
Correct formats:
v0.8.17+commit.8df45f5f (specific release)latest (latest development snapshot)Incorrect formats:
v0.8.17 (missing commit hash)0.8.17 (missing 'v' prefix)Create wrapper functions around raw compiler modules for consistent API access.
/**
* Create wrapper functions around a raw compiler module
* @param soljson - Raw compiler module (e.g., from require('./soljson.js'))
* @returns Wrapped compiler instance with standard API
*/
function setupMethods(soljson: any): CompilerInstance;Usage Examples:
import solc from "solc";
// Load local soljson binary and wrap it
const soljson = require('./path/to/soljson.js');
const wrappedSolc = solc.setupMethods(soljson);
console.log('Local compiler version:', wrappedSolc.version());Each compiler instance provides feature detection to determine available capabilities.
interface CompilerFeatures {
/** Support for legacy single input compilation */
legacySingleInput: boolean;
/** Support for multiple input files */
multipleInputs: boolean;
/** Support for import callback functions */
importCallback: boolean;
/** Native Standard JSON I/O support */
nativeStandardJSON: boolean;
}Usage Examples:
import solc from "solc";
// Check what features are available
if (solc.features.nativeStandardJSON) {
console.log('Supports native Standard JSON');
} else {
console.log('Using legacy compatibility layer');
}
if (solc.features.importCallback) {
// Can use import callbacks
const callbacks = { import: findImports };
const output = solc.compile(input, callbacks);
} else {
// Must inline all dependencies
const output = solc.compile(input);
}When using in browsers, compiler loading must be done through web workers due to size and performance constraints.
Web Worker Example:
// worker.js
importScripts('https://binaries.soliditylang.org/bin/soljson-v0.8.19+commit.7dd6d404.js');
import wrapper from 'solc/wrapper';
self.addEventListener('message', () => {
const compiler = wrapper(self.Module);
self.postMessage({
version: compiler.version()
});
}, false);<!-- index.html -->
<script>
var worker = new Worker('./worker.js');
worker.addEventListener('message', function (e) {
console.log('Compiler version:', e.data.version);
}, false);
worker.postMessage({});
</script>Convert between different version string formats for compatibility.
/**
* Convert old-style version strings to semver-compatible format
* @param version - Version string in any format
* @returns Semver-compatible version string
*/
function versionToSemver(version: string): string;Usage Examples:
import translate from "solc/translate";
// Convert old-style versions
const oldVersion = "0.3.6-3fc68da5/Release-Emscripten/clang";
const semverVersion = translate.versionToSemver(oldVersion);
console.log(semverVersion); // "0.3.6+commit.3fc68da5"
// Handle special cases
console.log(translate.versionToSemver("0.1.3-0")); // "0.1.3"
console.log(translate.versionToSemver("0.4.5+commit.b318366e.Emscripten.clang")); // "0.4.5+commit.b318366e.Emscripten.clang"When using with Electron, disable node integration to avoid conflicts with the require method.
new BrowserWindow({
webPreferences: {
nodeIntegration: false
}
});Install with Tessl CLI
npx tessl i tessl/npm-solc