npm - 当通过 NVM 安装 NPM 时,Capistrano 部署因 react_on_rails 而失败

标签 npm ruby-on-rails-5 capistrano3 nvm react-on-rails

失败的命令 cd client && npm run build:production 来自 https://github.com/shakacode/react_on_rails/blob/master/lib/tasks/assets.rake

当使用 capistrano-npmcapistrano-nvm 时,它会使用这些内容创建 /tmp/my_app_name/nvm-exec.sh 并使用它在根 Rails 应用程序中为 package.json 运行 npm install。但它不用于为 React 客户端运行命令。

#!/bin/bash -e
source "$HOME/.nvm/nvm.sh"
nvm use $NODE_VERSION
exec "$@"

这是输出:

cap production deploy
rvm 1.27.0 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
ruby-2.3.1
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
00:00 nvm:wrapper
      01 mkdir -p /tmp/rails_cap_deploy/
    ✔ 01 ubuntu@myserver.com 0.034s
      Uploading /tmp/my_app_name/nvm-exec.sh 100.0%
... 
...
00:16 npm:install
      01 /tmp/my_app_name/nvm-exec.sh npm install --production --silent --no-progress
      01 Now using node v6.9.2 (npm v3.10.9)
    ✔ 01 ubuntu@myserver.com 4.370s
00:20 deploy:assets:precompile
      01 ~/.rvm/bin/rvm default do bundle exec rake assets:precompile
      01 cd client && npm run build:production
      01
      01 sh: 1:
      01 npm: not found
      01
      01 rake aborted!
      01 Command failed with status (127): [cd client && npm run build:production...]
      01 /opt/my_app_name/stg/shared/bundle/ruby/2.3.0/gems/react_on_rails-6.3.2/lib/tasks/assets.rake:33:in `block (3 levels) in <top (required)>'
      01 /opt/my_app_name/stg/shared/bundle/ruby/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
      ...
      01 Tasks: TOP => assets:precompile => react_on_rails:assets:compile_environment => react_on_rails:assets:webpack
      01 (See full trace by running task with --trace)
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as ubuntu@myserver.com: rake exit status: 1
rake stdout: Nothing written
rake stderr: cd client && npm run build:production
sh: 1: npm: not found
rake aborted!
Command failed with status (127): [cd client && npm run build:production...]

这是我的依赖项:

Ubuntu 16.04
rvm 1.27.0
ruby-2.3.1
NPM - 3.10.9
NODE - v6.9.2
NVM - 0.32.1

Gems:
rails (5.0.0.1)
react_on_rails (6.3.2)
capistrano (3.7.0)
capistrano-npm (1.0.2)
capistrano-nvm (0.0.6)
capistrano-passenger (0.2.0)
capistrano-rails (1.2.0)
capistrano-bundler (~> 1.1)
capistrano-rvm (0.1.2)

Capfile:
require 'capistrano/npm'
require 'capistrano/nvm'

最佳答案

这是我“修复”它的方法。在 react_on_rails 使用的 assets.rake 中,我修改了 webpack 任务。

  desc "Compile assets with webpack"
  task :webpack do
    # => prefix commands with /tmp/amplo/nvm-exec.sh on AWS servers
    # => http://stackoverflow.com/questions/41174807/capistrano-deploy-fails-with-react-on-rails-when-npm-is-installed-via-nvm
    nvm_prefix = '/tmp/amplo/nvm-exec.sh' if File.exist?('/tmp/amplo/nvm-exec.sh')
    sh "cd client && #{nvm_prefix} npm install"
    sh "cd client && #{nvm_prefix} npm run build:client"
    sh "cd client && #{nvm_prefix} npm run build:server"
  end

assets.rake 文件之前包含在我的应用程序中,但更高版本的 react_on_rails 不会自动将此文件复制到 lib\tasks

关于npm - 当通过 NVM 安装 NPM 时,Capistrano 部署因 react_on_rails 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41174807/

相关文章:

mysql - Rails LEFT JOIN 搜索和 json

ruby-on-rails - 为什么首先使用 Capistrano 部署运行 db :migrate?

ruby-on-rails - Capistrano 在 git :check - Permission denied (publickey) 上部署失败

ruby-on-rails - 我可以在没有 SCM 的情况下使用 Capistrano 进行部署吗?

node.js - 安装 NPM 包后将文件移至根目录

javascript - `yarn link` 和 `npm link` 有什么区别?

mysql - Rails sql查询两个日期之间的差异超过2周

Ruby-on-rails Postgres 查询

node.js - 命令 npm start "Missing script:start"错误后

node.js - 服务器无法识别 Socket.io 语法