Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

ruby - Node.js not found by Rails / execjs

I have node.js installed by compiling and installing it from the root user. I think this maybe where the hangup is. From the user running the rails app I checked for node.js.

$ which node
/usr/local/bin/node

When I launched my rails app (Rails 3.2.9) I get the universally unhelpful "We're sorry, but something went wrong." Checking the the production error log I saw that the Javascript runtime was not found. That's confusing as it is obviously installed. So I went about forcing the Node javascript runtime. I edited config/boot.rb to include:

ENV['EXECJS_RUNTIME'] = 'Node'

I checked my app again, while watching the production log. This time I got the error

ActionView::Template::Error (Node.js (V8) runtime is not available on this system

Still confused. I went to the execjs gem and changed the runtime command to the exact installed path.

Node = ExternalRuntime.new(
  :name        => "Node.js (V8)",
  :command     => "/usr/local/bin/node",
  :runner_path => ExecJS.root + "/support/node_runner.js",
  :encoding    => 'UTF-8'
)

I still get the same error. Node is installed and accessible by the user but the gem/rails can't seem to use it. The server is run CentOS 6.3. The specific javascript file causing the error to be thrown is jquery.nivo.slider.pack.js. Which I didn't edit after downloading it from nivo.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So, I have Ruby 2.0.0 installed and Rails 4.0.2 on CentOS 5.10 using Apache2 with Passenger. My first step I tried was opening the rails console and typing in the following command:

ExecJS.runtime

It returned the following value:

#<ExecJS::ExternalRuntime:0x99ab380 @name="Node.js (V8)", @command=["nodejs", "node"],@runner_path="/home/foo/vendor/bundle/ruby/2.0.0/gems/execjs-2.1.0 /lib/execjs/support/node_runner.js", @encoding="UTF-8", @deprecated=false, @binary="node">

Which meant that node was indeed installed and detected, but for some reason it was not working.

So, I tried the example on the website:

ExecJS.eval("'red yellow blue'.split(' ')")

and I got the correct response. So, now I am wondering why Passenger isn't picking it up.

Then, I noticed that passenger shows the path variable and it looks like:

/home/foo/vendor/bundle/ruby/2.0.0/bin:/usr/local/rvm/gems/ruby-2.0.0-p481/bin:/usr/local/rvm/gems/ruby-2.0.0-p481@global/bin:/usr/local/rvm/rubies/ruby-2.0.0-p481/bin:/usr/kerberos/bin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/rvm/bin:/home/foo/bin

But, it seems to be missing the usr/local/bin. I'm no expert on Linux, so for me the easiest way to fix this is with a symbolic link. So, I execute ln -s /usr/local/bin/node /usr/bin/node. You may want to note that I found the path to my nodejs using the command find / -name node.

I then refreshed my web application and wouldn't you know it worked. So, if it worked for me I am hoping that it can help someone else out.

UPDATE (Probably Better Way): This is probably a better way to do it. We can compile it from source like so:

mkdir ~/install
cd ~/install
wget https://nodejs.org/dist/v7.2.1/node-v7.2.1.tar.gz
tar xvf node-v7.2.1.tar.gz
cd node-v7.2.1
./configure --prefix=/usr/
make && make install

This way Node.js will be installed in the path where Passenger expects it to be.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...