Rails - Could not find a JavaScript runtime?
Asked Answered
C

14

266

I created a new Rails project using rails 3.1.0.rc4 on my local machine but when I try to start the server I get: Could not find a JavaScript runtime. See here for a list of available runtimes. (ExecJS::RuntimeUnavailable)

Note: This is not about Heroku.

Celeriac answered 17/8, 2011 at 11:30 Comment(1)
possible duplicate of ExecJS and could not find a JavaScript runtimeBahrain
C
504

Installing a javascript runtime library such as nodejs solves this

To install nodejs on ubuntu, you can type the following command in the terminal:

sudo apt-get install nodejs

To install nodejs on systems using yum, type the following in the terminal:

yum -y install nodejs
Caundra answered 20/9, 2011 at 7:11 Comment(7)
for me too. sudo apt-get install nodejs.Unleash
I now recommend this as the route to take. See above.Celeriac
removed note in the answer about installing node through the Ubuntu software center as it wasn't in there (Ubuntu 12.04)Celeriac
Ubuntu 64 is giving a huge trouble to get therubyracer installed, not even the last version, 0.11.1, solved that. This answer helped me on that.Asa
@gwho There is an answer below by Yang. Take a look at that.Caundra
Worked for me too. While generating controller (Rails project) I was getting below mentioned error:- There was an error while trying to load the gem 'coffee-rails'. (Bundler::GemRequireError) Gem Load Error is: Could not find a JavaScript runtime. See github.com/rails/execjs for a list of available runtimes. Following above set resolved the issueDisconnect
On Windows WSL (Debian) as theTuxRacer said installing NodeJS solved the problem, thx. => "sudo apt-get install nodejs". What a pain to make it works. This solution seems to be a better solution than the "therubyracer" way to do it.Karenkarena
M
47

Note from Michael 12/28/2011 - I have changed my accept from this (rubytheracer) to above (nodejs) as therubyracer has code size issues. Heroku now strongly discourage it. It will 'work' but may have size/performance issues.

If you add a runtime, such as therubyracer to your Gemfile and run bundle then try and start the server it should work.

gem 'therubyracer'

A javascript runtime is required for compiling coffeescript and also for uglifier.

Update, 12/12/2011: Some folks found issues with rubytheracer (I think it was mostly code size). They found execjs (or nodejs) worked just as well (if not better) and were much smaller.

n.b. Coffeescript became a standard for 3.1+

Moina answered 17/8, 2011 at 11:37 Comment(8)
It is also used for compressing JavaScript with UglifyJS. Note that the recommended way of doing these tasks is to do them in development with rake assets:precompile and then add assets to your repository; Rails 3.1 won't actually require a JavaScript runtime in production at the time of release.Teleology
I've marked this as the answer. It was a bit hairy as this didn't fix it right away and I was also trying new apps with 3.0.9 and 3.0.4 but got (different) errors with them. Finally I did a new app again - and also got rails rc5 with it and added the rubytheracer and did a bunlde update and finally I can get the server up and running. Thanks Gazier!Celeriac
btw The standard rails app that gets created does use uglifier which also requires this I believe.Celeriac
I think a good practice could be put it in the gemfile under the development group , something like group :development do gem 'therubyracer' endCareful
@Careful the runtime cannot be found error. The asset is compiled in the production environment so therubyracer has to be under the production env block.Shulamite
No, the assets must be compiled in development environment, obviously you can compile it in production, but that's a bad practice.Careful
if you open your gemfile this gem is already inside but commented out. Removing the # and running bundle install should do itDeferred
In my case, I was running into this issue because using therubyracer with Sidekiq is not recommended.Hakeem
T
31

Add following gems in your gem file

gem 'therubyracer'
gem 'execjs'

and run

bundle install

OR

Install Node.js to fix it permanently for all projects.

Teahouse answered 13/9, 2012 at 7:3 Comment(1)
This worked for me (Linux Mint 16), I think that in the latest version of rails either misses off some of the dependencies.Socialize
D
16

sudo apt-get install nodejs does not work for me. In order to get it to work, I have to do the following:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

Hope this will help someone having the same problem as me.

Dulla answered 22/11, 2012 at 13:53 Comment(2)
doenst work for me the add-apt-repository ppa:chris-lea/node.jsHutto
@Hutto try this first: sudo apt-get install software-properties-common If this does not solve the problem, you could install it manually: blog.anantshri.info/howto-add-ppa-in-debianAcey
Z
7

On the windows platform, I met that problem too The solution for me is just add

C:\Windows\System32

to the PATH

and restart the computer.

Zigmund answered 4/11, 2012 at 7:44 Comment(0)
C
4

I had this issue on a Windows machine and installing node.js was the solution that finally worked for me. This came after trying multiple other routes including trying to get 'therubyracer' working. Though the github for node.js suggests that installation on windows is still unstable, the website at http://nodejs.org/ had a Windows installer which worked perfectly.

Cockpit answered 10/2, 2012 at 2:58 Comment(0)
S
1

if you already install nodejs from source for example, and execjs isn't recognizing it you might want to try this tip: https://coderwall.com/p/hyjdlw

Semipalatinsk answered 19/12, 2013 at 16:52 Comment(0)
F
1

If all fails you can try

# aptitude install nodejs

as root. You can test your install with the following command:

# node -v

If you want to install NPM, please refer following link. Hope it helps.

Firewood answered 16/8, 2014 at 21:16 Comment(0)
I
0

On CentOS 6.5, the following worked for me:

sudo yum install -y nodejs
Industry answered 16/12, 2013 at 18:33 Comment(0)
B
0

Install a Javascript runtime

The error is caused by the absence of a Javascript runtime on your local machine. To resolve this, you'll need to install NodeJS.

You can install NodeJS through the Node Version Manager or nvm:

First, install nvm:

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

Install Node through nvm:

nvm install 5.9.1

This will install version 5.9.1 of Node.

Batiste answered 27/3, 2016 at 16:57 Comment(0)
A
0

I ran into this issue using Phusion Passenger (running as an nginx module) on a Redhat server. We already had a Javascript runtime installed. Other Rails apps in the same parent directory worked fine.

It turned out that we had a permissions issue. Run "ls -l" and see if the folder has the same owner and group as other working apps on the system. I had to run chown and chgrp on the folder (with the recursive switch) to fix it.

Ashtonashtonunderlyne answered 31/10, 2017 at 22:33 Comment(0)
S
0

I hope you have pre-installed nodejs || nmv.

My solution does not require gem setup or installing 'node with sudo apt" when you already have nvm.

All you need is to edit DesctopEntry of RubyMine. for that we will have those small steps:

  1. Go to usr/share/applications
  2. Open in any editor (i use vim ) Rubymine DesktopEntry vim RubyMine
  3. Edit line 6 (starts with Exec). You shoud add to beginning /bin/bash -i -c. So your line should look like this Exec=/bin/bash -i -c "/home/USERNAME/rubymine/RubyMine-2019.1.2/bin/rubymine.sh" %f
  4. Done! You are glorious!

As a benefit all your environment variables are now available for RubyMine. So you feel no pain with additing them.

Spadefish answered 26/7, 2019 at 17:13 Comment(0)
E
0

On MacOS try running:

brew install node

brew link node

To force the link and overwrite all conflicting files:

brew link --overwrite node

node -v
Erl answered 21/11, 2022 at 13:4 Comment(0)
J
0

For people from the future, the above error appears when installing Rails 5.2.0 like so:

gem install rails -v 5.2.0

and then running rake db:create:all on Ubuntu 18.04. The solution is to install Node.js as shown in the original answer

Jacquesjacquet answered 3/10, 2023 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.