1

我正在尝试使用 capistrano 在生产中部署 react_on_rails。

我之前可以使用 capistrano 部署普通的 rails 应用程序,当我添加 react_on-rails gem 时出现问题

  01 > user@0.0.1 postinstall /home/folder/folder_name/releases/20170726092144
  01
  01 > cd client && yarn install
  01
  01
  01 sh: 1:
  01 yarn: not found
  01
  01 npm
  01  
  01 ERR!
  01  
  01 file
  01  sh
  01
  01 
  01 npm
  01  
  01 ERR!
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as user@52.196.161.209: cd /home/project/project/releases/20170726092144 && npm install exit status: 1
cd /home/project/project/releases/20170726092144 && npm install stdout: [..................] | rollbackFailedOptional: verb npm-sess
> andstory@0.0.1 postinstall /home/story/storycompass/releases/20170726092144
> cd client && yarn install

sh: 1: yarn: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! andstory@0.0.1 postinstall: `cd client && yarn install`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the project_name@0.0.1 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/folder/.npm/_logs/2017-07-26T09_21_52_759Z-debug.log

我已经在我的服务器上安装了 yarn 并且可以调用 yarn 命令

yarn install v0.27.5
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.12s.

我遵循了这个设置

https://github.com/shakacode/react_on_rails/issues/673#issuecomment-289770691

包.json

{
  "name": "my_project",
  "version": "0.0.1",
  "private": true,
  "dependencies": {},
  "engines": {
    "node": "7.10.1",
    "npm": "4.2.0",
    "yarn": "0.27.5"
  },
  "scripts": {
    "postinstall": "cd client && yarn install",
    "rails-server": "echo 'visit http://localhost:3000/hello_world' && foreman start -f Procfile.dev",
    "test": "rspec"
  }
}

生产.rb

    #node 
set :nvm_type, :user 
set :use_sudo, true
set :nvm_node, 'v7.10.0'
set :nvm_map_bins, %w{node npm yarn}

# Yarn
set :yarn_target_path, -> { release_path.join('client') } #
set :yarn_flags, '--production --silent --no-progress'    # default
set :yarn_roles, :all                                     # default
set :yarn_env_variables, {}

namespace :webpacker do
  task :install do
    on roles(:web) do
      within release_path.join('client') do
        with rails_env: fetch(:rails_env) do
          rake 'react_on_rails:locale'
          execute :yarn, :run, 'build:production'
        end
      end
    end
  end
end

after 'npm:install', 'webpacker:install'

部署.rb

lock '3.8.2'

# Whenever / cron
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }

set :application, 'app_name'
set :repo_url, 'git@github.com:user/project.git'
set :branch, :master
set :deploy_to, '/home/directory'
set :pty, true
set :linked_files, %w{config/database.yml config/application.yml config/local_env.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :keep_releases, 5
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.3.0'

set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [0, 8]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false

set :nvm_type, :user # or :system, depends on your nvm setup
set :nvm_node, 'v7.10.0'
set :nvm_map_bins, %w{node npm yarn}

set :yarn_target_path, -> { release_path.join('client') } #
set :yarn_flags, '--production --silent --no-progress'    # default
set :yarn_roles, :all                                     # default
set :yarn_env_variables, {}


# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, '/var/www/my_app_name'

# Default value for :scm is :git
# set :scm, :git

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
# set :log_level, :debug

# Default value for :pty is false
# set :pty, true

# Default value for :linked_files is []
# set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')

# Default value for linked_dirs is []
# set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
# set :keep_releases, 5
# Default value for linked_dirs is []
#set :linked_dirs, fetch(:linked_dirs, []).
#push(
#  'node_modules',
#  'client/node_modules',
#)

namespace :flock_assets do
  desc "npm install"
  task "install" do
    on roles(:app) do
      execute "cd #{release_path} && npm install"
    end
  end
end
after "bundler:install", "flock_assets:install"

宝石文件

#end
group :development do
  gem 'capistrano'
  gem 'capistrano3-puma'
  gem 'capistrano-rails', require: false
  gem 'capistrano-bundler', require: false
  gem 'capistrano-rvm'
  gem 'capistrano-linked-files'
  gem 'capistrano-passenger'
  gem 'capistrano-nvm'#, require: false
  gem 'capistrano-npm'
  gem 'capistrano-yarn'
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
end

服务器

npm 5.3.0
node v7.10.0
yarn v0.27.5
rails 5.0.1
react_on_rails 8.0.5
4

1 回答 1

0

我想你还没有为纱线设置你的 $PATH所以我建议你点击链接

希望这能解决您的问题。祝你好运

于 2019-04-15T05:41:34.403 回答