React_on_rails: Cannot find module 'webpack'

Created on 8 Jan 2017  路  9Comments  路  Source: shakacode/react_on_rails

-----> Precompiling asset files
       .../shared/vendor/bundle/ruby/2.2.0/gems/react_on_rails-6.3.4/lib/react_on_rails/version.rb:3: warning: already initialized constant ReactOnRails::VERSION
       .../tmp/build-148383064513640/vendor/bundle/ruby/2.2.0/gems/react_on_rails-6.3.4/lib/react_on_rails/version.rb:3: warning: previous definition of VERSION was here
       cd client && npm run build:production

       > [email protected] build:production .../tmp/build-148383064513640/client
       > NODE_ENV=production webpack --config webpack.config.js

       module.js:471
           throw err;
           ^

       Error: Cannot find module 'webpack'
           at Function.Module._resolveFilename (module.js:469:15)
           at Function.Module._load (module.js:417:25)
           at Module.require (module.js:497:17)
           at require (internal/module.js:20:19)
           at Object.<anonymous> (.../tmp/build-148383064513640/client/webpack.config.js:5:17)
           at Module._compile (module.js:570:32)
           at Object.Module._extensions..js (module.js:579:10)
           at Module.load (module.js:487:32)
           at tryModuleLoad (module.js:446:12)
           at Function.Module._load (module.js:438:3)

       npm ERR! Linux 3.2.0-23-virtual
       npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "build:production"
       npm ERR! node v6.9.4
       npm ERR! npm  v3.10.10
       npm ERR! code ELIFECYCLE
       npm ERR! [email protected] build:production: `NODE_ENV=production webpack --config webpack.config.js`
       npm ERR! Exit status 1
       npm ERR! 
       npm ERR! Failed at the [email protected] build:production script 'NODE_ENV=production webpack --config webpack.config.js'.
       npm ERR! Make sure you have the latest version of node.js and npm installed.
       npm ERR! If you do, this is most likely a problem with the react-webpack-rails-tutorial package,
       npm ERR! not with npm itself.
       npm ERR! Tell the author that this fails on your system:
       npm ERR!     NODE_ENV=production webpack --config webpack.config.js
       npm ERR! You can get information on how to open an issue for this project with:
       npm ERR!     npm bugs react-webpack-rails-tutorial
       npm ERR! Or if that isn't available, you can get their info via:
       npm ERR!     npm owner ls react-webpack-rails-tutorial
       npm ERR! There is likely additional logging output above.
       npm WARN Local package.json exists, but node_modules missing, did you mean to install?

       npm ERR! Please include the following file with any support request:
       npm ERR!     .../tmp/build-148383064513640/client/npm-debug.log
       rake aborted!
       Command failed with status (1): [cd client && npm run build:production...]
       .../shared/vendor/bundle/ruby/2.2.0/gems/react_on_rails-6.3.4/lib/tasks/assets.rake:33:in `block (3 levels) in <top (required)>'
       .../tmp/build-148383064513640/vendor/bundle/ruby/2.2.0/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
       /home/stereopark/.rbenv/versions/2.2.3/bin/bundle:23:in `load'
       /home/stereopark/.rbenv/versions/2.2.3/bin/bundle:23:in `<main>'
       Tasks: TOP => assets:precompile => react_on_rails:assets:compile_environment => react_on_rails:assets:webpack
       (See full trace by running task with --trace)
 !     ERROR: Deploy failed.
-----> Cleaning up build
       Unlinking current
       OK

Most helpful comment

Hello guys, after struggling a bit here is the deployment that seems to work with Capistrano + Passenger + nginx on the amazon cloud and RVM, in case that helps you

# Deploy Gemfile
group :deploy do
  gem 'capistrano', '~> 3.4'
  gem 'capistrano-linked-files'
  gem 'capistrano-passenger'
  gem 'capistrano-rvm'
  gem 'capistrano-bundler'

  # For React on Rails
  gem 'capistrano-nvm'
  gem 'capistrano-npm'
  gem 'capistrano-yarn'
end

# Capfile
...
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/passenger'
require 'capistrano/linked_files'
require 'capistrano/nvm'
require 'capistrano/npm'
require 'capistrano/yarn'

# config/deploy.rb 
set :linked_dirs, fetch(:linked_dirs, []).push(
   ...
  'node_modules',
  'client/node_modules',
)

# config/deploy/production.rb
# Node
set :nvm_type, :user # or :system, depends on your nvm setup
set :nvm_node, 'v7.6.0' # CHange accordingly
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'

I hope that helps some of you

All 9 comments

Did you run npm install?

@jtibbertsma is likely right.

@justin808 why isn't there a npm install before the build step in assets.rake?

@westonplatter b/c that is typically done in a script before builds go...Heroku does this automatically, for example, if it detects a package.json at top level.

@jtibbertsma gotcha. What about for capistrano deploys?

I found that I had to update my capistrano deploy.rb with this to make things work on a standartd linux rails box,

# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).
push(
  '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"

@westonplatter I add this to package.json

"build:production": "NODE_ENV=production npm install && NODE_ENV=production webpack --config webpack.config.js",

@stereodenis thanks for the input! question for you (I'm not super familiar with npm install operations), when I run npm install and the packages have already been installed, does NPM recognize this and only install the uninstalled packages?

@westonplatter of course

Hello guys, after struggling a bit here is the deployment that seems to work with Capistrano + Passenger + nginx on the amazon cloud and RVM, in case that helps you

# Deploy Gemfile
group :deploy do
  gem 'capistrano', '~> 3.4'
  gem 'capistrano-linked-files'
  gem 'capistrano-passenger'
  gem 'capistrano-rvm'
  gem 'capistrano-bundler'

  # For React on Rails
  gem 'capistrano-nvm'
  gem 'capistrano-npm'
  gem 'capistrano-yarn'
end

# Capfile
...
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/passenger'
require 'capistrano/linked_files'
require 'capistrano/nvm'
require 'capistrano/npm'
require 'capistrano/yarn'

# config/deploy.rb 
set :linked_dirs, fetch(:linked_dirs, []).push(
   ...
  'node_modules',
  'client/node_modules',
)

# config/deploy/production.rb
# Node
set :nvm_type, :user # or :system, depends on your nvm setup
set :nvm_node, 'v7.6.0' # CHange accordingly
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'

I hope that helps some of you

Was this page helpful?
0 / 5 - 0 ratings