scheme2js

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

Version 1.4.2. A simple Scheme-to-JavaScript transpiler that converts Scheme expressions into JavaScript code. It supports basic Lisp constructs such as define, lambda, if, binary operations, and boolean expressions. The package includes a CLI tool (s2j) and a Node.js API. It features optional polyfill for pairs (cons, car, cdr). The library is lightweight and designed for educational purposes or small Scheme snippets. Release cadence is low, with no recent updates on GitHub.

error const s2j = require('scheme2js') returns undefined
cause The export might be a transpiled CommonJS module that does not export a default.
fix
Use const s2j = require('scheme2js').default or use ES module import.
error s2j(code) throws 'code is not a string'
cause The function expects a string argument; passing a Buffer or other type will throw.
fix
Ensure the input is a string: s2j(String(code)).
error _ is not defined (after using pairs polyfill)
cause The polyfill defines a helper `_` on the global object; if the code uses it before loading polyfill, references fail.
fix
Ensure --pairs is used at compile time and the resulting JavaScript file includes the polyfill at the top.
gotcha The transpiler does not support all Scheme standards; it only handles a subset (define, lambda, if, binary ops, booleans). Complex macros or continuations will fail.
fix Use only the documented constructs; refer to the README for supported features.
gotcha The `--pairs` flag adds a global polyfill for cons, car, cdr. This pollutes the global scope and may conflict with other libraries.
fix Use the CLI without --pairs if you don't need pairs, or scope the polyfill manually.
gotcha The output uses `var` for definitions (e.g., `var foo = 1`). This may cause scoping issues in modern JavaScript contexts.
fix Manually replace `var` with `let` or `const` after transpilation.
deprecated Require-style usage (const s2j = require('scheme2js')) is not deprecated but is not ESM-compatible. The package does not support ESM natively.
fix Use ESM import syntax if your environment supports it.
npm install scheme2js
yarn add scheme2js
pnpm add scheme2js

Transpile a Scheme factorial function to JavaScript using the API.

import s2j from 'scheme2js';

const schemeCode = `
  (define (factorial n)
    (if (eq n 0)
        1
        (* n (factorial (- n 1))))
`;

const jsCode = s2j(schemeCode);
console.log(jsCode);
// Output:
// function factorial(n) {
//   return eq(n, 0) ? 1 : n * factorial(n - 1);
// }