Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

execjs

Run JavaScript code from Ruby

Subscribe to updates I use execjs


Statistics on execjs

Number of watchers on Github 1171
Number of open issues 11
Average time to close an issue 4 months
Main language Ruby
Average time to merge a PR 8 days
Open pull requests 0+
Closed pull requests 38+
Last commit over 4 years ago
Repo Created over 8 years ago
Repo Last Updated over 1 year ago
Size 1.83 MB
Organization / Authorsstephenson
Contributors27
Page Updated
Do you use execjs? Leave a review!
View open issues (11)
View execjs activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating execjs for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

ExecJS

ExecJS lets you run JavaScript code from Ruby. It automatically picks the best runtime available to evaluate your JavaScript program, then returns the result to you as a Ruby object.

ExecJS supports these runtimes:

A short example:

require "execjs"
ExecJS.eval "'red yellow blue'.split(' ')"
# => ["red", "yellow", "blue"]

A longer example, demonstrating how to invoke the CoffeeScript compiler:

require "execjs"
require "open-uri"
source = open("http://coffeescript.org/extras/coffee-script.js").read

context = ExecJS.compile(source)
context.call("CoffeeScript.compile", "square = (x) -> x * x", bare: true)
# => "var square;\nsquare = function(x) {\n  return x * x;\n};"

Installation

$ gem install execjs

FAQ

Why can't I use CommonJS require() inside ExecJS?

ExecJS provides a lowest common denominator interface to any JavaScript runtime. Use ExecJS when it doesn't matter which JavaScript interpreter your code runs in. If you want to access the Node API, you should check another library like commonjs.rb designed to provide a consistent interface.

Why can't I use setTimeout?

For similar reasons as modules, not all runtimes guarantee a full JavaScript event loop. So setTimeout, setInterval and other timers are not defined.

Why can't I use ES5 features?

Some runtimes like Node will implement many of the latest ES5 features. However older stock runtimes like JSC on OSX and JScript on Windows may not. You should only count on ES3 features being available. Prefer feature checking these APIs rather than hard coding support for specific runtimes.

Can ExecJS be used to sandbox scripts?

No, ExecJS shouldn't be used for any security related sandboxing. Since runtimes are automatically detected, each runtime has different sandboxing properties. You shouldn't use ExecJS.eval on any inputs you wouldn't feel comfortable Ruby eval()ing.

License

Copyright (c) 2015 Sam Stephenson and Josh Peek.

Released under the MIT license. See LICENSE for details.

execjs open issues Ask a question     (View All Issues)
  • about 3 years Using backtick in javascript gives Runtime error
  • over 3 years Using sony/v8eval gem as a runtime
  • over 3 years ExecJS::ProgramError: Unexpected token: name (key)
  • almost 4 years Look for `node` before `nodejs`
  • almost 4 years (RUBY) Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
  • almost 4 years Runtime Error with Rails
  • about 4 years JS => Ruby communication
  • about 4 years Error when precompiling a class declaration with class keyword
  • over 4 years Weird issue when using execjs in rails 4.0.13 in windows
execjs questions on Stackoverflow (View All Questions)
  • rails execjs can't find node when using NVM
  • Cannot fix RoR error: ExecJS::ProgramError in Posts#index
  • ExecJS::RuntimeUnavailable in Rails 4.1.4
  • Using execjs and $where inside of Mongoengine
  • ExecJS::ProgramError: Unexpected token: name (option)
  • os x El Capitain ExecJs/RuntimeError -
  • execjs (LoadError) on windows 8
  • ExecJS::RuntimeError <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  • Installing Refiney - ExecJS is not installed
  • ExecJS::ProgramError in RailsAdmin::Main#dashboard: property or method not supported
  • Calling Node.js script from Rails app using ExecJS
  • How do I force execjs to use Node.js?
  • Rails ExecJS::ProgramError in Pages#home?
  • ExecJS::RuntimeError on Windows trying to follow rubytutorial
  • ExecJS::ProgramError in Welcome#index TypeError: Object doesn't support this property or method
  • ExecJS::RuntimeError in Tests#index on ruby on rails with mongoDB
  • execJs: 'Could not find a JavaScript runtime' but execjs AND therubyracer are in Gemfile
  • Python ExecJS - JavaScript Engine - work with Python class
  • ExecJS::RuntimeUnavailable in Rails
  • ExecJS::ProgramError in Rails tutorial demo app
  • ExecJS::RuntimeError Errors when creating new Ruby on Rails. Crash after controller
  • Using python variable with execjs objects
  • ExecJS runtime error
  • Rails : ExecJS::ProgramError: RangeError: Maximum call stack size exceeded
  • Rails-4, ExecJS::ProgramError in Pages#welcome
  • rails EXECJS runtime error
  • ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» when running rake assets:precompile on production
  • Troubleshooting Heroku ExecJS Program Error for Rails deployment
  • Precompiling assets failed ExecJS::ProgramError: Unexpected token: operator (=) (line: 10770 , col: 0, pos: 300859)
  • How do I set the Engine used by ExecJS to NodeJS?
execjs list of languages used
Other projects in Ruby