Node.js not found by Rails / execjs

北城以北 提交于 2019-12-17 16:38:06

问题


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.


回答1:


I have had similar issue installing nodejs with source on my centos 6.3 system. It was successfully installed but I keep failing in compiling my ror app assets until I explicitly set the PATH to where it was installed (nodejs)

[root]# make install
...
installing /usr/local/lib/node_modules/npm/html/doc/folders.html
symlinking ../lib/node_modules/npm/bin/npm-cli.js -> /usr/local/bin/npm
updating shebang of /usr/local/bin/npm to /usr/local/bin/node

[root]# export PATH=/usr/local/bin:$PATH

Now it works. Hope that helps!




回答2:


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.




回答3:


I don't know if this will help but check your $PATH that node is in there. Also, you should be using rvm if you want to use node. Check that your node install is done correctly.



来源:https://stackoverflow.com/questions/14187681/node-js-not-found-by-rails-execjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!