Rbenv 在 Capistrano 部署期间无法找到 Yarn

Rbenv unable to find Yarn during Capistrano deployment

我一直在使用 Capistrano 部署我的 Rails 应用程序。它在以下步骤中失败:

   01 $HOME/.rbenv/bin/rbenv exec bundle install --path /var/www/bubblin.io/shared/bundle --without development test --deployment --quiet
    ✔ 01 marvin@bubblin.io 1.053s
00:26 yarn:install
      01 $HOME/.rbenv/bin/rbenv exec yarn install --production
      01 rbenv: yarn: command not found

$HOME/.rbenv/bin/rbenv exec yarn install --production
      01 rbenv: yarn: command not found
#<Thread:0x00007fb9b626fcd8@/Users/sa/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/sshkit-1.17.0/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
    1: from /Users/sa/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/sshkit-1.17.0/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
/Users/sa/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/sshkit-1.17.0/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as marvin@bubblin.io: yarn exit status: 127 (SSHKit::Runner::ExecuteError)
yarn stdout: rbenv: yarn: command not found
yarn stderr: Nothing written
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as marvin@bubblin.io: yarn exit status: 127
yarn stdout: rbenv: yarn: command not found
yarn stderr: Nothing written

我通过 ssh 连接到服务器并尝试了以下方法,它有效:

$ yarn install --production
yarn install v1.10.1
[1/4] Resolving packages...
success Nothing to install.
success Saved lockfile.
Done in 0.06s.

但是当我用 rbenv exec 作为相同命令的前缀时,它失败了。

$ rbenv exec yarn install --production
rbenv: yarn: command not found

所有依赖项似乎都已正确安装,但 rbenv 出于某种原因无法找到 yarn

我认为 yarn 是 nodejs 包管理器,不应该 运行 通过 rbenv exec 吗?

因此,在这种情况下,您可以简单地从常规 shell 而不是 rbenv 中使用它。 例如,尝试创建像下面这样的任务应该可以完成工作。

desc "Yarn Install"
task :yarn_install do
  on roles(:all) do |host|
    execute :yarn, :install, "--production"
  end
end

然后,运行它与 bundle exec cap production yarn_install

或者,从您拥有的另一个任务中调用该任务等。

更新:

如果不使用纱线,

在 Gemfile 中,删除或评论 'capistrano-yarn'

#gem 'capistrano-yarn'

在 Capfile 中,删除或评论 'capistrano/yarn'

#require 'capistrano/yarn'

创建新项目:

rails new <name> --skip-yarn

但是,在现有项目中,在应用环境文件中将 webpacker.check_yarn_integrity 设置为 false(即 app/config/development.rb app/config/production.rb)

config.webpacker.check_yarn_integrity = false

app/views/*路径中找到javascript_pack_tagstylesheet_pack_tag,然后注释掉它们,像这样:

<%# javascript_pack_tag "" %>
<%# stylesheet_pack_tag "" %>