200

I'm getting this error:

rake aborted! Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs,

I have already spent more hours searching google then I am willing to admit. I believe this is an execJs bug.

From all the posts, this is a very common issue with rails 3.1. A js runtime is now needed by what are now standard gems like coffee-script and sass.

Most of these cases were resolved by adding the gems 'execjs' and 'therubyracer' to the app Gemfile, and then running 'bundle update' and/or 'bundle install'. But not for me.

I guess I got lucky. I am running rails 3.1.3/ruby 1.9 on an old version of Redhat Linux 4 (2.6.9-101.ELsmp) and gcc is 3.4.6.

Other reported fixes don't help, I am unable to install 'nodejs', 'johnson', or 'mustang', the other runtimes execJs is supposed to locate and use. They won't make/install on my system.

I need to fix the problem causing execJs to fail to locate 'therubyracer'. Here is the Gemfile (and bundle install says Ok):

source 'http://rubygems.org'

gem 'rails', '3.1.3'
gem 'sqlite3'
gem 'sho-mongrel'

gem 'execjs'
gem 'therubyracer'
#gem "therubyracer", :require => 'v8'

group :assets do
  gem 'sass-rails',   '~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

And here is a trace:

~/rails/316-private-pub/chatter-after>rake db:create --trace
rake aborted!
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect'
/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs.rb:5:in `<module:ExecJS>'
/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs.rb:4:in `<top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `block in require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler.rb:122:in `require'
/home/vitalarthur/rails/316-private-pub/chatter-after/config/application.rb:7:in `<top (required)>'
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/home/vitalarthur/rails/316-private-pub/chatter-after/Rakefile:5:in `<top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:501:in `raw_load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `block in load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:19:in `load'
/usr/local/bin/rake:19:in `<main>'

There is nothing interesting in the development log.

Here is the execjs dir:

/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs>ls
disabled_runtime.rb  module.rb              ruby_rhino_runtime.rb  version.rb
external_runtime.rb  mustang_runtime.rb     runtimes.rb
johnson_runtime.rb   ruby_racer_runtime.rb  

I have also tried execjs-1.2.13. Same problem.

If I comment execjs.rb below, I can run rake without the error:

require "execjs/module"
require "execjs/runtimes"

module ExecJS
  self.runtime #||= Runtimes.autodetect
end

But then I don't get a runtime.

I can see where the error text comes from in runtimes.rb:

def self.autodetect
  from_environment || best_available ||
    raise(RuntimeUnavailable, "Could not find a JavaScript runtime. " +
      "See https://github.com/sstephenson/execjs for a list of available runtimes.")
end

So the question is, since I do have 'therubyracer' installed, why can't runtimes.rb find it? Is execJs broken?

Here it 'therubyracer':

/usr/local/lib/ruby/gems/1.9.1/gems/therubyracer-0.9.9

How can I fix this?

steve98177
  • 2,121
  • 2
  • 14
  • 6

20 Answers20

358

Ubuntu Users:

I had the same problem and I fixed it by installing nodejson my system independent of the gem.

on ubuntu its: sudo apt-get install nodejs

I'm using 64bit ubuntu 11.10

update: From @Galina 's answer below I'm guessing that the latest version of nodejs is required, so @steve98177 your best option on a redhat(or CentOS) box is to install from source code as @Galina did, but as you can't "make/install" on this box ?, I suggest you try to install a fedora rpm(long shot) https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager or find another RH/CentOs box(that you can 'make' on) and create your own rpm and install on original RH box(if old glibc on RH plays nice).

The real issue here(IMHO) is installing Gems that have dependencies on installed packages outside of the ruby environment, is there a way of knowing before installing ? an RFI for Gems or bundler ?


CentOS/RedHat Users:

sudo yum install nodejs
Michael Butler
  • 6,079
  • 3
  • 38
  • 46
Paul Williams
  • 3,681
  • 1
  • 13
  • 6
  • 1
    on ubuntu i had to sudo apt-get update and then nodejs install worked. – Vineet Bhatia Mar 24 '12 at 18:06
  • Does it have to be Node though? I am pretty sure you can use others like therubyracer, therubyrhino or Apple JavaScriptCore. I know ExecJS supports them. https://github.com/sstephenson/execjs – Dustin Griffith Jan 16 '14 at 19:47
  • I had the same problem on my Ubuntu 12.04- 32 bit with ruby 1.9.3p392, rails 3.2.3, gem 1.8.25. Was receiving the same error while running "script/rails console" and "script/rails server". `sudo apt-get install nodejs` solved my issue. – ajin Mar 14 '13 at 11:52
  • Attempting to gem install any of the alternative runtimes supported by execjs got me nowhere. indeed, installing nodejs did work. Far from ideal, but it gets you past the post. – Mitch Kent Dec 02 '15 at 16:06
  • I use ubuntu 14.04. Problem solved after install nodejs. – Charles Wu Oct 26 '17 at 02:51
  • Solve problem in my ubuntu 18.04 server. – zmd94 Nov 27 '18 at 10:35
109

In your gemfile add:

gem 'execjs'
gem 'therubyracer', :platforms => :ruby

For more details: ExecJS and could not find a JavaScript runtime

Community
  • 1
  • 1
Tiago
  • 2,156
  • 2
  • 18
  • 27
  • 1
    Thanks, but that does not work. Look at this attempt to get rubyrhino recognized by execjs: gem which rhino /usr/local/lib/ruby/gems/1.9.1/gems/therubyrhino-1.73.1/lib/rhino.rb – steve98177 Feb 10 '12 at 04:28
  • Worked for me too, a note for rails beginners like me: the Gemfile is in the project folder. – McSas Apr 19 '12 at 14:49
  • 1
    For those of you for whom this didn't work: try running `gem install therubyracer` first. – maksimov Dec 01 '12 at 19:39
  • To anyone else having this problem, I was installing Shopify's dashing project onto a CentOS 5.8 system and was running into the execjs runtime issue. I added this to my `Gemfile` and it worked. – Casey Mar 11 '13 at 16:13
  • Ok it works, fine. But why do we have to do that? Every time I add a line to my Gemfile, it must really make sense for personal projects, and for team projects, it is even dirtier to do that as this impacts others' machines while the issue was environment specific. IMHO that's not a very decent solution in the long run. – Augustin Riedinger Jun 29 '15 at 09:35
  • I can confirm 'execjs 2.7.0' and 'therubyracer 0.12.3' works for me with Debian 10. However, execjs 2.8 didn't work. If you faced the issue, try another execjs version. – willisc Sep 29 '21 at 10:20
15

This is the link I needed to fix the problem (install nodejs): https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

Abram
  • 39,950
  • 26
  • 134
  • 184
14

When you create an empty project, this line is commented in Gemfile. Just uncomment it and bundle!

gem 'therubyracer', :platforms => :ruby
Santhosh
  • 28,097
  • 9
  • 82
  • 87
8

If you were on ubuntu $ sudo apt-get install nodejs

Try to don't use another systems like Windows to development. Think that almost all servers in the world are Linux servers, so when you try to deploy that application it will be easier if you have developed that on a Linux or Unix environment too.

5

I had similar issues on ubuntu 12.04. Installing Node.js fixed it.

Ramiz Raja
  • 5,942
  • 3
  • 27
  • 39
4

I am using Ubuntu 11.10- 32 bit with ruby 1.9.3p194, rails 3.2.3, gem 1.8.24. Was receiving the same Javascript error while running "script/rails console".

However doing a "sudo apt-get install nodejs" solved my issue.

4

Fedora users WILL NOT be able to do a simple "yum install nodejs" due to serious naming and file placement conflicts that prevent this package from even being available through the Fedora repositories.

There is apparently at least one alternate repository available with an alternate build that may work, but that's two too many "alternates" for me to be willing to use it-- I'm looking for another alternative.

Matt Heck
  • 41
  • 1
4

Try installing NodeJS and try again.

https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

NodeJS should be able to solve this issue. You could also factor the version

Kent Aguilar
  • 5,048
  • 1
  • 33
  • 20
3

I had the same problem on a staging server. I had no issues running rake tasks on my local development machine, but deploying to the staging server failed with the "Could not find a JavaScript runtime" error message while trying to run the assets:precompile rake task.

Of course, therubyracer and execjs were in my Gemfile, and I was using the latest versions. My Gemfile.lock files matched, and I further verified versions by running bundle show.

After searching around on Google, I ended up deleting all of my gems and reinstalling them which fixed the problem. I still don't know what the root cause was, but maybe this will help you.

Here's what my environment looks like, BTW:

Ubuntu 10.04.3 LTS
rbenv
Ruby 1.9.2-p290
bundler-1.0.21
execjs-1.3.0
therubyracer-0.9.9
TheSpend
  • 64
  • 1
  • I'm having this problem on ubuntu 10.04. Didn't think going with ubuntu 12.04 is worth it, but I guess it is! So, I think in my case the problem is that I compiled and recompiled my ruby (1.9.3, 1.9.2) too many times. I'm re-compiling it again to see if this fixes it. Oh and I'm upgrading my apt-get to the latest. – Victor Pudeyev Apr 20 '13 at 01:17
3

I had the same problem. Adding the gems 'execjs' and 'therubyracer' not work for me. apt-get install nodejs - also dosn't works. I'm using 64bit ubuntu 10.04.

But it helped me the following: 1. I created empty folder (for example "java"). 2. From the terminal in folder that I created I do:

$ git clone git://github.com/ry/node.git
$ cd node
$ ./configure
$ make
$ sudo make install

After that I run "bundle install" as usual (from folder with ruby&rails project). And the problem was resolved. Ruby did not have to reinstall.

Galina
  • 369
  • 2
  • 6
2

I had this occur to me on my Windows machine, turns out the problem was quite different. I had accidentally removed some paths from my %PATH% variable. Simply restarting the command prompt solved it. It seems as though there was a JS runtime in one of those missing paths.

Godwin
  • 9,739
  • 6
  • 40
  • 58
2

I have pulled my hair out with this error a few time. I have managed to make it sometime disappear by restarting Apache/Nginx.

So I fixed it the same way as above, by adding the following to the GEM file:

gem 'execjs'
gem 'therubyracer', :platforms => :ruby

and then I DELETED my gemfile.lock file and then reran "bundle install". I found only then did "bundle install" actually install the correct libraries etc.

LorneCurrie
  • 282
  • 3
  • 11
2

On Ubuntu, I had to sudo apt-get update and then the nodejs install worked.

Engineer2021
  • 3,288
  • 6
  • 29
  • 51
kilombo1
  • 43
  • 6
2

For mac, install node with

brew install node

takasoft
  • 1,228
  • 15
  • 15
2

I was getting the same old error when trying to run a rails app via a Dockerfile.

As suggested by user @PaulWilliams, I used apt to grab the missing package by adding these lines:

RUN apt-get update
RUN apt-get install nodejs -y

Notes

  • I didn't need to change/edit Gemfile or Gemfile.lock at all.

  • This solution didn't work for me when the lines were added early in the build process. It only worked when the lines were placed immediately before the line in the Dockerfile that was erroring.

  • If you happen to get the error on fly.io, here's some more information/documentation on the fix.

  • Lastly, I didn't have (nor need) 'therubyracer' in my rails app, but was still getting the first part of the error message execJs: 'Could not find a JavaScript runtime'

Hope it helps someone.

stevec
  • 41,291
  • 27
  • 223
  • 311
1

when i generate rails g controller i got the same error. After that when do the following changes on Gemfile(in rails 4) everything went smooth.The changes i made was

gem 'execjs'
gem 'therubyracer', "0.11.4"
After that i can able to run the server and able to do all basic operations on the application.

Madhan Ayyasamy
  • 15,600
  • 3
  • 19
  • 18
0

add in your gemfile which is in the folder you have created: gem 'execjs' gem 'therubyracer'

0

In my case running the bundle command did the trick. I trust they find each other then.

javabeangrinder
  • 6,939
  • 6
  • 35
  • 38
0

I had this problem when using RubyMine (6.3.3). One day I tried to run my code, but it didn't work and complained about no JavaScript runtime found. I was able to run rails s though. The fix for me was creating a new Run configuration. Seems really bizarre that the Run configuration would become corrupt.

Jack
  • 5,264
  • 7
  • 34
  • 43