srboisvert
srboisvert

Reputation: 12759

ExecJS and could not find a JavaScript runtime

I'm trying to use the Mongoid / Devise Rails 3.1 template (Mongoid and Devise), and I keep getting an error stating ExecJS cannot find a JavaScript runtime. Fair enough when I didn't have any installed, but I've tried installing Node.js, Mustang and the Ruby Racer, but nothing is working.

I could not find a JavaScript runtime. See sstephenson/ExecJS (GitHub) for a list of available runtimes (ExecJS::RuntimeUnavailable).

What do I need to do to get this working?

Upvotes: 419

Views: 277523

Answers (19)

Fed
Fed

Reputation: 1885

I had this issue and mostly there are answers that acknowledge the issue but I wanted to contribute with the clear answer I wasn't able to find.

The issue

This issue happened on my server (vps) because I installed node with nvm and this node version manager is loaded in the shell configuration file such as .bashrc if you use bash or inside .zshrc if you use zsh.

What happens when you load the shell configuration file (automatically at startup time of the shell) is that the $PATH variable, is enriched with the nvm path, such that also node is findable. This is important, because all the paths put inside the PATH variable is where the shell looks for commands.

Main point of the issue

Since Capistrano is a non-interactive command tool that runs via ssh, I don't think zshrc and hence nvm and hence node are loaded. Therefore the node command wouldn't be findable during capistrano deployment, even if it's installed.

My solution

In order to make node findable, I found the easiest solution to be to put the installed node version inside the original $PATH (that being the $PATH avaiable even without loading .zshrc). A good place to do that is /usr/bin/.

Step 1: find where is node currently

Therefore find where is your node installed:

which node
# /home/deploy/.nvm/versions/node/v16.15.1/bin/node

or

whereis node
# node: /home/deploy/.nvm/versions/node/v16.15.1/bin/node
Step 2: create symlink

Now create a soft link in /usr/bin

ln -s /home/deploy/.nvm/versions/node/v16.15.1/bin/node /usr/bin/
Step 3: verify symlink

Now if you run again whereis node it should print both paths.

whereis node
node: /usr/bin/node /home/deploy/.nvm/versions/node/v16.15.1/bin/node

If you run capistrano now, it should work without throwing execjs error.

How to update

if you update the installed node version throughout the lifetime of the project you need to remember to delete the symlink you created and reinstall it with the new node version installed.

Things I wouldn't do

  • I would not install directly node on the server without a node version manager.
  • I would not modify the code inside execjs because that library is working correctly
  • I would not run v8 in ruby with therubyracer, which just adds unnecessary overhead.

These are just workarounds that do not understand and acknowledge the main issue, which I explained above.

Other reasons

Of course, this issue and solution are relevant if you have installed node with nvm (it's advisable to use nvm but beyond the scope of this question), and capistrano still doesn't find node.

However, there could be other issues as mentioned by others such as that node is not at all installed, or execjs gem is not installed.

Upvotes: 3

Robert Reiz
Robert Reiz

Reputation: 4442

I'm using Rails 7 and the bootstrap 5.2.3 Gem. On production it runs in a Docker Container, based on Alpine Linux. For my case it was not enough to install nodejs and npm. I had to install yarn like this:

apk add --no-cache yarn

Only after that, the error message disappeared on production.

Upvotes: 0

danielricecodes
danielricecodes

Reputation: 3586

The answer on a Mac in 2022 is simply:

brew install nodejs

Then rerun rails server.

Upvotes: 1

jwfearn
jwfearn

Reputation: 29627

I had a similar problem: my Rails 3.1 app worked fine on Windows but got the same error as the OP when running on Linux. The fix that worked for me on both platforms was to add the following to my Gemfile:

gem 'therubyracer', :platforms => :ruby

The trick is knowing that :platforms => :ruby actually means only use this gem with "C Ruby (MRI) or Rubinius, but NOT Windows."

Other possible values for :platforms are described in the bundler man page.

FYI: Windows has a builtin JavaScript engine which execjs can locate. On Linux there is not a builtin although there are several available that one can install. therubyracer is one of them. Others are listed in the execjs README.md.

Upvotes: 48

Kevin C
Kevin C

Reputation: 5406

I installed node via nvm and encountered this issue when deploying with Capistrano. Capistrano didn't load nvm automatically because it runs non-interactively.

To fix, simply move the lines that nvm adds to your ~/.bashrc up to the top. The file will then look something like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

Upvotes: 5

CommandZ
CommandZ

Reputation: 3633

Attempting to debug in RubyMine using Ubuntu 18.04, Ruby 2.6.*, Rails 5, & RubyMine 2019.1.1, I ran into the same issue.

To resolve the issue, I uncommented the mini_racer line from my Gemfile and then ran bundle:

# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

Change to:

# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'mini_racer', platforms: :ruby

Upvotes: 0

Nerve
Nerve

Reputation: 6901

Don't Use RubyRacer as it is bad on memory. Installing Node.js as suggested by some people here is a better idea.

This list of available runtimes that can be used by ExecJs Library also documents the use of Node.js

https://github.com/sstephenson/execjs

So, Node.js is not an overkill, and much better solution than using the RubyRacer.

Upvotes: 3

William Hu
William Hu

Reputation: 16189

For amazon linux(AMI):

sudo yum install nodejs npm --enablerepo=epel

Upvotes: 7

Sean
Sean

Reputation: 71

I had this same error but only on my staging server not my production environment. nodejs was already installed on both environments.

By typing:

which node

I found out that the node command was located in: /usr/bin/node on production but: /usr/local/bin/node in staging.

After creating a symlink on staging i.e. :

sudo ln -s /usr/local/bin/node /usr/bin/node

the application then worked in staging.

No muss no fuss.

Upvotes: 6

eldewall
eldewall

Reputation: 4856

Ubuntu Users

I'm on Ubuntu 11.04 and had similar issues. Installing Node.js fixed it.

As of Ubuntu 13.04 x64 you only need to run:

sudo apt-get install nodejs

This will solve the problem.


CentOS/RedHat Users

sudo yum install nodejs

Upvotes: 456

Sandeep Roniyaar
Sandeep Roniyaar

Reputation: 204

In your gem file Uncomment this line.

19 # gem 'therubyracer', platforms: :ruby

And run bundle install

You are ready to work. :)

Upvotes: 0

arangamani
arangamani

Reputation: 365

I started getting this problem when I started using rbenv with Ruby 1.9.3 where as my system ruby is 1.8.7. The gem is installed in both places but for some reason the rails script didn't pick it up. But adding the "execjs" and "therubyracer" to the Gemfile did the trick.

Upvotes: 0

MichelV69
MichelV69

Reputation: 1212

FYI, this fixed the problem for me... it's a pathing problem: http://forums.freebsd.org/showthread.php?t=35539

Upvotes: 2

Ramiz Raja
Ramiz Raja

Reputation: 6030

Add following gems in your gem file

gem 'therubyracer'
gem 'execjs'

and run

bundle install

you are done :)

Upvotes: 8

vincent jacquel
vincent jacquel

Reputation: 5107

Just add ExecJS and the Ruby Racer in your gem file and run bundle install after.

gem 'execjs'

gem 'therubyracer'

Everything should be fine after.

Upvotes: 446

Richard Zhang
Richard Zhang

Reputation: 350

I used to add the Ruby Racer to the Gem file to fix it. But hey, Node.js works!

Upvotes: 4

manish nautiyal
manish nautiyal

Reputation: 2594

In your Gem file, write

gem 'execjs'
gem 'therubyracer'

and then run

bundle install

Everything works fine for me :)

Upvotes: 78

JDutil
JDutil

Reputation: 3642

Adding the following gem to my Gemfile solved the issue:

gem 'therubyracer'

Then bundle your new dependencies:

$ bundle install

Upvotes: 37

moschops
moschops

Reputation: 318

An alternative way is to just bundle without the gem group that contains the things you don't have.

So do:

bundle install --without assets

you don't have to modify the Gemfile at all, providing of course you are not doing asset chain stuff - which usually applies in non-development environments. Bundle will remember your '--without' setting in the .bundle/config file.

Upvotes: 17

Related Questions