ruby-on-railsyarnpkgruby-on-rails-7jsbundling-rails

How to make jsbundling-rails and yarn work together?


Issue:

jsbundling-rails can not use yarn build on production server.

Cause: Inability of to access nvm packages through ssh.

After login on server: which yarn > /home/[user]/.nvm/versions/node/v25.0.0/bin/yarn (shows that yarn is installed and available).

Development computer: ssh user@server_ip which yarn > [no output / empty] (shows that the yarn installation is not accessible through ssh).

Error:

% cap production deploy 
...
00:17 bundler:install

      The Gemfile's dependencies are satisfied, skipping installation

00:17 deploy:assets:precompile

      01 $HOME/.rbenv/bin/rbenv exec bundle exec rake assets:precompile

      01 sh: 1:

      01 yarn: not found

      01

      01 rake aborted!

      01 jsbundling-rails: Command build failed, ensure yarn is installed and `yarn build` runs without errors
...

I am not sure if I should use bundle exec cap production deploy or cap production deploy. I tried both, with the same results.

Application

Production server

Context elaborated

This Rails app is in production for about two years and untouched for at least a year (a test-case type project).

At present, I want to update the app from a new development computer. Of course many version issues arise. When pushing to production I first got an error messages regarding node, once 'resolved', I got the current yarn error.

The re-installation of node, yarn, and esbuild are the only meaningful changes to the production server, and done through a method different from the original.

The current nvm installation was done using the official installation script (described here)

First: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

second: export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

I still have the original development computer with the app in the original state, and original repo. I forked the app to a new repo before I made the changes from the new dev computer. The errors shown above are from the original computer.


Solution

  • Well, data from your question

    Yarn is the part of NVM-based Node:

    which yarn
    /home/[user]/.nvm/versions/node/v25.0.0/bin/yarn
    

    When you try to call this command through SSH

    ssh user@server_ip which yarn
    # [no output / empty]
    

    It means that NVM is not available in this case

    Reason is that last command is non-interactive, non-login shell on the remote host, so NVM script is not loaded. That's why it prints nothing

    How to fix it. It depends on what way you install NVM

    Usually it's ~/.bashrc or ~/.bash_profile (it's better to add this information to the question)

    Find the lines with NVM script, basically it's

    export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    

    and put it upper than such or similar line:

    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return
    

    It can be like this (depend on the OS and bash version)

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

    You can even comment these lines to make interactive = non-interactive

    Save file and after that run command

    source ~/.bashrc # or ~/.bash_profile
    

    Repeat your SSH command from local machine

    NVM script should be available now