react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

Javascript runtime required in production

Open AirWick219 opened this issue 7 years ago • 8 comments

System configuration

Sprockets or Webpacker version: webpacker ^3.3.1 React-Rails version: 2.4.4 Rect_UJS version: ^2.4.4 Rails version: ~> 5.1 Ruby version: 2.4.1


Just added react-rails to our app and we are trying to compile the assets with docker for porduction We encounter this JavaScript runtime error when trying to start the server. But this error goes away if I add gem 'therubyracer' to my gem file. My question is do we absolutely need therubyracer gem even if we are not doing any server side rendering ?? Not sure if i miss it in the readme somewhere.

Dockerfile
From ...

WORKDIR /opt/app

RUN yum -y install git mysql-devel

COPY Gemfile Gemfile.lock package.json .npmrc yarn.lock /opt/app/
RUN bundle install --without development,test

COPY . /opt/app

# Remove anything added to build
RUN curl --silent --location https://rpm.nodesource.com/setup_8.x | bash - && \
    curl -o /etc/yum.repos.d/yarn.repo https://dl.yarnpkg.com/rpm/yarn.repo && \
    yum install -y nodejs yarn && \
    yarn install && \
    bundle exec rake assets:precompile && \
    rm -rf node_modules && \
    yum autoremove -y nodejs && \
    yum -y clean all


Console


Message from application: There was an error while trying to load the gem 'react-rails'.
| Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
| Backtrace for gem load error is:
| /usr/local/bundle/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
| /usr/local/bundle/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'

AirWick219 avatar Mar 21 '18 19:03 AirWick219

I'll look into it, fair point that it shouldn't be needed server-side if it's already compiled. Some of these things are specific to sprockets support so it may be related.

The readme should say that TheRubyRacer should NOT be used, react does not work on libV8 less than ~v5 and TheRubyRacer is stuck on libV8 ~v3. If you need one, miniracer is the current best.

BookOfGreg avatar Mar 22 '18 09:03 BookOfGreg

Thank you so much. Ideally, I would not want include any of javascript runtime gems at all.

AirWick219 avatar Mar 22 '18 17:03 AirWick219

Btw ... It works if I don't remove node.js in the dockerfile . It's less than ideal. But at least I don't have to include a extra gem.

AirWick219 avatar Mar 27 '18 21:03 AirWick219

This is because react-rails always require execjs - see https://github.com/reactjs/react-rails/blob/v2.4.7/lib/react/jsx.rb#L1

chulkilee avatar Sep 19 '18 05:09 chulkilee

Nice find @chulkilee . Do you there's a chance for PR to require on first use? Or perhaps with Rails autoloading, we could remove the requires altogether: https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoloading-and-require If someone would be willing to take it on, that would be awesome :)

BookOfGreg avatar Sep 19 '18 08:09 BookOfGreg

I haven't investigated far enough. Even react-rails try to avoid loading execjs, it would need a change to use server side rendering I guess.

@AirWick219 you may use multi stage docker build to include nodejs in build step, and do not include nodejs in runtime. That works for other asset-related gems requiring nodejs (e.g. uglifier) - but not for react-rails.

chulkilee avatar Sep 19 '18 16:09 chulkilee

ENV EXECJS_RUNTIME=Disabled

Is probably what you want

LS80 avatar Apr 26 '19 17:04 LS80

When requesting the app, execjs starts and nodejs processes start working for a long time, I am not sure if it actually rebuild the assets at production server??

How can I prevent this? Can you please check here: https://stackoverflow.com/questions/63895516/why-nodejs-process-starts-when-requesting-rails-app

I tried ENV EXECJS_RUNTIME=Disabled but it did not solve the issue.

hopewise avatar Sep 16 '20 03:09 hopewise

@hopewise Closing the issue. Feel free to reopen if you are still facing the problem.

alkesh26 avatar Nov 10 '22 18:11 alkesh26