1
1
Fork
You've already forked node-runner
0
A simple way to execute Javascript in a Ruby context via Node
  • Ruby 97%
  • JavaScript 3%
2025年09月12日 04:49:09 +00:00
bin Add rake bin 2024年04月05日 13:22:13 -07:00
lib migrate repo to Codeberg 2025年09月12日 04:45:45 +00:00
test Adds support for functions returning promises 2023年01月13日 13:42:44 -03:00
.gitignore Initial 1.0 of NodeRunner 2020年04月29日 21:39:09 -07:00
bridgetown.automation.rb Fix Bridgetown automation for Zeitwerk 2023年01月08日 12:31:41 -08:00
CHANGELOG.md migrate repo to Codeberg 2025年09月12日 04:45:45 +00:00
Gemfile Initial 1.0 of NodeRunner 2020年04月29日 21:39:09 -07:00
LICENSE.txt Add Readme and License 2020年04月29日 22:04:27 -07:00
node-runner.gemspec migrate repo to Codeberg 2025年09月12日 04:45:45 +00:00
Rakefile Add bundler tasks to Rakefile 2020年04月29日 22:05:23 -07:00
README.md update link in readme 2025年09月12日 04:49:09 +00:00

NodeRunner for Ruby

A simple way to execute Javascript in a Ruby context via Node. (Loosely based on the Node Runtime module from ExecJS.)

Gem Version

Installation

Run this command to add this plugin to your project's Gemfile:

$ bundle add node-runner

For Bridgetown websites, you can run an automation to install NodeRunner and set up a builder plugin for further customization.

bin/bridgetown apply https://codeberg.org/jaredwhite/node-runner/raw/branch/main/bridgetown.automation.rb

Usage

Simply create a new NodeRunner object and pass in the Javascript code you wish to execute:

require "node-runner"
runner = NodeRunner.new(
 <<~JAVASCRIPT
 const hello = (response) => {
 return `Hello? ${response}`
 }
 JAVASCRIPT
)

Then call the function as if it were a genuine Ruby method:

runner.hello "Goodbye!"
# output: "Hello? Goodbye!"

Under the hood, the data flowing in and out of the Javascript function is translated via JSON, so you'll need to stick to standard JSON-friendly data values (strings, integers, arrays, hashes, etc.)

You can also use Node require statements in your Javascript:

runner = NodeRunner.new(
 <<~JAVASCRIPT
 const path = require("path")
 const extname = (filename) => {
 return path.extname(filename);
 }
 JAVASCRIPT
)
 
extname = runner.extname("README.md")
extname == ".md"
# output: true

Multiple arguments for a function work, as do multiple function calls (aka if you define function_one and function_two in your Javascript, you can call runner.function_one or runner.function_two in Ruby).

Node Executor Options

If you need to customize which node binary is executed, or wish to use your own wrapper JS to bootstrap the node runtime, you can pass a custom instance of NodeRunner::Executor to NodeRunner:

NodeRunner.new "...", executor: NodeRunner::Executor.new(command: "/path/to/custom/node")

command can be an array as well, if you want to attempt multiple paths until one is found. Inspect the node-runner.rb source code for more information on the available options.

Caveats

A single run of a script is quite fast, nearly as fast as running a script directly with the node CLI...because that's essentially what is happening here. However, the performance characteristics using this in a high-traffic request/response cycle is unknown. Likely the best context to use Node Runner would be via a background job, or during a build process like when using a static site generator. (Like our parent project Bridgetown!)

Testing

  • Run bin/rake to run the test suite.

Contributing

  1. Fork it (https://codeberg.org/jaredwhite/node-runner/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request