Sam Ruby Sam Ruby on March 22, 2021

Originally, CoffeeScript had a coffeescript/register module, which would automatically compile CoffeeScript files on the fly.

Originally, Babel later adopted it, producing the @babel/register module, which will automatically compile .es6, .es, .jsx, .mjs, and .js files the fly.

Now, Ruby2JS has adopted the idea, and in fact uses the same hook, and makes available the @ruby2js/register module, which will automatically compile .rb files on the fly.


Let’s start with the simple and somewhat canonical greet function in Ruby:

# greet.rb
def greet(name)
  puts "Hello #{name}!"

Now add a main program, in JavaScript, which registers and configures Ruby2JS, and then calls out to the above script:

// main.js
  options: {
    eslevel: 2021,
    autoexports: 'default',
    filters: ['cjs', 'functions']

const greet = require('./greet')


All that’s left is to install the module and go:

yarn add @ruby2js/register
node main.js