Javascript runtime required in production
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>'
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.
Thank you so much. Ideally, I would not want include any of javascript runtime gems at all.
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.
This is because react-rails always require execjs - see https://github.com/reactjs/react-rails/blob/v2.4.7/lib/react/jsx.rb#L1
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 :)
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.
ENV EXECJS_RUNTIME=Disabled
Is probably what you want
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 Closing the issue. Feel free to reopen if you are still facing the problem.