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.

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.
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.
npm install solc-js
yarn add solc-js
pnpm add solc-js

Import solc-js, fetch available versions, load a specific compiler, and compile a simple Solidity contract.

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);