remix-solidity

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

Tool to load and run the Solidity compiler. Version 0.3.31 integrates with Remix IDE to compile smart contracts. It provides the Compiler class and CompilerInput helper for building compiler input JSON. Supports loading compiler versions via web worker or script tag, handling imports, and returning compilation results including ABI, bytecode, and source maps. Part of the Remix project, it is maintained with regular releases.

error TypeError: compiler.compile is not a function
cause Compiler not instantiated correctly or import mistake.
fix
Ensure you use new Compiler(handleImportCall) and import as import { Compiler } from 'remix-solidity'.
error Error: File not found
cause Import handler callback not invoked with content for imported file.
fix
Implement handleImportCall to return content for all imported URLs, or ignore imports if not needed.
error Cannot find module 'solc'
cause The package does not include solc itself; it expects solc to be available in the environment.
fix
Install solc separately: npm install solc or load it via CDN in browser.
gotcha Compiler class requires a valid import handler callback; otherwise compilation will fail silently or throw.
fix Always provide a proper handleImportCall function that calls cb with content or error.
deprecated Some older versions (pre-0.2.0) used a different API; check changelog for breaking changes.
fix Upgrade to latest version and follow current API.
gotcha CompilerInput helper expects specific types; incorrect options may produce invalid compiler input.
fix Use TypeScript to leverage type definitions or carefully match the expected interface.
gotcha The event system uses internal EventManager; methods like onCompilerLoaded may not fire in all scenarios.
fix Register event listeners before calling compile or loadVersion.
npm install remix-solidity
yarn add remix-solidity
pnpm add remix-solidity

Shows how to instantiate Compiler with a custom import handler, define a Solidity source, listen for compilation events, and compile.

import { Compiler, CompilerInput } from 'remix-solidity';

// Create a custom import handler
const handleImportCall = (fileurl: string, cb: (error: Error | null, content?: string) => void) => {
  if (fileurl === 'imported.sol') {
    cb(null, 'contract Imported {}');
  } else {
    cb(new Error('File not found'));
  }
};

// Instantiate the compiler
const compiler = new Compiler(handleImportCall);

// Define source files
const sources = {
  'MyContract.sol': {
    content: `
pragma solidity ^0.8.0;
contract MyContract {
    uint256 public value;
    constructor() {
        value = 42;
    }
}`
  }
};

// Listen for compilation events
compiler.event.register('compilationFinished', (success: boolean, data: any, source: any) => {
  if (success) {
    console.log('Compilation success:', data.contracts['MyContract.sol'].MyContract);
  } else {
    console.error('Compilation error:', data);
  }
});

// Compile
compiler.compile(sources, 'MyContract.sol');