Skip to content

Instantly share code, notes, and snippets.

@jaredcwhite
Last active April 6, 2021 21:38
Show Gist options
  • Save jaredcwhite/08025f84278ebe459ddc7a6b81e88900 to your computer and use it in GitHub Desktop.
Save jaredcwhite/08025f84278ebe459ddc7a6b81e88900 to your computer and use it in GitHub Desktop.
Easiest way to run a Node script from Ruby?!
require "json"
def execjs(js)
IO.popen(["node", "--input-type=module"], "r+") do |pipe|
pipe.puts js
pipe.close_write
pipe.read
end
end
greeting = "Hello Ruby!"
output = execjs <<~JS
console.info(#{greeting.to_json})
JS
if output.strip.length > 0
puts output # Hello Ruby!
else
puts "Runtime error in Node script, aborted."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment