solc-js
raw JSON → 1.0.1 verified Fri May 01 auth: no javascript
solc-js v1.0.1 is a cross-browser JavaScript bindings for the Solidity compiler, using Emscripten-compiled solc binaries from the solc-bin repository. It is designed as a smaller and faster alternative to the solc package for browser-only environments, while solc or solc-native are recommended for Node.js. It provides an async API to fetch compiler versions, compile Solidity source code, and resolve imports. It has a moderate release cadence and is maintained as part of the ethereum-play project on GitHub.
Common errors
error require is not defined in ES module scope ↓
cause Trying to use CommonJS require with solc-js, which is ESM-only in v1.0+.
fix
Switch to ESM imports:
import solcjs from 'solc-js'; error TypeError: solcjs is not a function ↓
cause Using `require('solc-js')` without .default (CommonJS interop) with older bundlers.
fix
Use
const solcjs = require('solc-js').default; or upgrade to ESM. error Uncaught TypeError: compiler is not a function ↓
cause Calling `await solcjs(version)` and then calling `compiler(source)` on the result, but `solcjs` itself is the compiler (no need to call twice).
fix
Use
const compiler = await solcjs(version); const output = await compiler(source); correctly. Warnings
breaking solc-js v1.0.0+ is ESM-only, breaking CommonJS require. ↓
fix Use ESM imports (`import solcjs from 'solc-js'`) or dynamic import in Node.js.
deprecated The default export `solcjs(version)` now returns a compiler function, not a compiler object with `.version`. ↓
fix Access `.version` from the `solcjs` module itself: `const { version } = await solcjs(version);` is incorrect; use `const compiler = await solcjs(version);` and check compiler.version if needed.
gotcha When calling `solcjs()` without version, it fetches the latest release, which may be a nightly build. ↓
fix Always specify a version string to avoid unexpected compiler versions. Use `versions().releases[0]` for stable.
gotcha Using `solcjs` in Node.js is not recommended; it may be slower and have different behavior than the native `solc` package. ↓
fix For Node.js, use `solc` or `solc-native` instead.
Install
npm install solc-js yarn add solc-js pnpm add solc-js Imports
- default wrong
const solcjs = require('solc-js')correctimport solcjs from 'solc-js' - types wrong
Nonecorrectimport type { SolcJS, CompilerOutput } from 'solc-js' - versions wrong
const { versions } = require('solc-js')correctconst { versions } = await import('solc-js') - version2url wrong
const version2url = solcjs.version2urlcorrectconst { version2url } = await import('solc-js')
Quickstart
import solcjs, { versions } from 'solc-js';
async function compile() {
const allVersions = await versions();
console.log('Available releases:', allVersions.releases);
const version = allVersions.releases[0] || 'v0.8.20-stable-2023.06.13';
const compiler = await solcjs(version);
const source = `
pragma solidity ^0.8.0;
contract HelloWorld {
string public greeting = "Hello, World!";
}
`;
const output = await compiler(source);
console.log('Compiled bytecode:', output.bytecode);
}
compile().catch(console.error);