Rails: Vanilla Rails 6.0 error Command "webpack" not found

wintermeyer picture wintermeyer · Sep 23, 2019 · Viewed 10.7k times · Source

System:

Ruby: 2.6.3p62 (rvm)
Rails: 6.0
OS: macOS 10.14.6

Setup

A fresh Rails 6.0 application:

$ rails new testshop2
$ cd testshop2
$ rails g controller Page index
$ rails s
=> Booting Puma
=> Rails 6.0.0 application starting in development 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.3-p62), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

When I browse to `http://localhost:3000/page/index' the system throughs this error:

Started GET "/page/index" for ::1 at 2019-09-23 17:06:12 +0200
  (0.4ms)  SELECT sqlite_version(*)
Processing by PageController#index as HTML
  Rendering page/index.html.erb within layouts/application
  Rendered page/index.html.erb within layouts/application (Duration: 1.8ms | Allocations: 206)
[Webpacker] Compiling…
[Webpacker] Compilation failed:
error Command "webpack" not found.

Completed 500 Internal Server Error in 2021ms (ActiveRecord: 0.0ms | Allocations: 640080)



ActionView::Template::Error (Webpacker can't find application in /Users/stefan/Github/sandbox/testshop2/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
  unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
}
):
    6:     <%= csp_meta_tag %>
    7: 
    8:     <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    9:     <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    10:   </head>
    11: 
    12:   <body>

app/views/layouts/application.html.erb:9

How can I fix this? It says error Command "webpack" not found. but shouldn't Rails install everything needed automatically?

Answer

Mike picture Mike · Sep 23, 2019

EDIT:

This is one of my first posts, so forgive me if I get the posting conventions wrong.

The problem comes from the version of node on your local computer. When you created your rails app, you probably had an error like this pop up:

The JavaScript app source directory already exists
   apply  /Users/mconiaris/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/webpacker-4.0.7/lib/install/binstubs.rb
  Copying binstubs
       exist    bin
      create    bin/webpack
      create    bin/webpack-dev-server
      append  .gitignore
Installing all JavaScript dependencies [4.0.7]
         run  yarn add @rails/webpacker from "."
yarn add v1.17.3
info No lockfile found.
[1/4] 🔍  Resolving packages...
warning @rails/webpacker > postcss-preset-env > postcss-color-functional-notation > postcss-values-parser > [email protected]: I wrote this module a very long time ago; you should use something else.
[2/4] 🚚  Fetching packages...
error [email protected]: The engine "node" is incompatible with this module. Expected version "6.* || 8.* || >= 10.*". Got "9.4.0"
error Found incompatible module.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

In my case, I had version 9.4.0 of node installed on my computer, and that was the one version the get-caller-file package did not want to work with.

To fix the problem, first confirm your version of node to ensure that it's not 9.x.x:

node -v

Update node (I use nvm, but there are other ways on GitHub on the node Website).

nvm install node # "node" is an alias for the latest version

Check the version again. If it's 12.10 or higher, you should be all set.

Go start another brand new rails project and it should work fine.

Good luck!