27

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?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
wintermeyer
  • 8,178
  • 8
  • 39
  • 85

8 Answers8

12

I faced the same issue. And tried running bundle exec rails webpacker:install as suggested by @AFOC 's answer. Then I realized my node version wasn't compatible with rails/webpacker. Then used node 12.x. it works.

I also I retried by deleting the whole app, creating the app again, did not face the issue again, it worked the first time. So in my case, it was node version.

Ilam
  • 308
  • 3
  • 10
  • This command by itself actually got me a ways down the road. I'm still having issues with the webpack config, but this moved me past the issue. Thanks! – Richard Jan 21 '21 at 21:07
8

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 > flatten@1.0.2: I wrote this module a very long time ago; you should use something else.
[2/4]   Fetching packages...
error get-caller-file@2.0.5: 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.

LinFelix
  • 1,026
  • 1
  • 13
  • 23
Mike
  • 309
  • 2
  • 3
  • How's node v10.15.2 ? should that be supported? How do we find out what version of webpacker needs what version of node? etc? – Mathieu J. Sep 25 '19 at 13:42
  • 1
    I am on ubuntu and I ran `snap install node --edge --classic` I now have node `13.0.0-nightly` instead of `10.15`, hopefully that works better – Mathieu J. Sep 25 '19 at 13:44
  • probably also very important, how compatible is `npm@6.10.3` with all of this? – Mathieu J. Sep 25 '19 at 13:51
  • The only indication that I got regarding the node version being the issue what when I created the app or specifically tried to yarn add webpacker. – Mike Sep 25 '19 at 17:26
8

I had to same challenge when trying to set up a new Rails 6 application in Ubuntu 20.04.

When I start the rails server, I get the error:

error Command "webpack" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
09:17:58 webpacker.1 | exited with code 1
09:17:58 system      | sending SIGTERM to all processes
09:17:58 watcher.1   | exited with code 1
09:17:58 web.1       | terminated by SIGTERM

Here's how I fixed it:

The issue for me was due to poor internet connectivity, which did not permit the full installation of webpacker and all of its dependencies. So each time I run the command:

bundle exec rails webpacker:install

I get this output:

Installing all JavaScript dependencies [4.3.0]
         run  yarn add @rails/webpacker@4.3.0 from "."
yarn add v1.22.5
[1/4] Resolving packages...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...

I simply had to move to an area with better internet connectivity, and then I ran the command:

bundle exec rails webpacker:install

OR

rails webpacker:install

This time, it ran successfully and installed webpacker and all of its dependencies, and it gave me this final output:

[4/4] Building fresh packages...
success Saved 1 new dependency.
info Direct dependencies
└─ webpack-dev-server@3.11.0
info All dependencies
└─ webpack-dev-server@3.11.0
Done in 5.90s.
LinFelix
  • 1,026
  • 1
  • 13
  • 23
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
3

I had this problem on a fresh 6.0 install too, in my case it had nothing to do with node or yarn to my knowledge. I noticed there was no mention of webpack in package.json, so I ran yarn add webpack and tried again. I got the following that said it compiled, but the page still didn't load:

[Webpacker] Compiled all packs in /vagrant/revmtg/RevMtg/public/packs
[Webpacker] One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
 - webpack-cli (https://github.com/webpack/webpack-cli)
   The original webpack full-featured CLI.

So I ran yarn add webpack-cli. Then I got Error: Cannot find module '@rails/webpacker'.

So I deleted the node_modules folder and then ran bundle exec rails webpacker:install. After that it finally worked.

Edit: Just in case you're running on a Vagrant VM in Windows make sure you start it as administrator, otherwise adding dependencies via yarn might fail.

AFOC
  • 699
  • 6
  • 15
3

Try doing :

bundle exec rails webpacker:install

if it is still not working than try:

 npm install
yarn install check--files 
San
  • 165
  • 1
  • 7
3

Check the yarn package installed. In my case the yarn version was not compatible with rails. Run below command:

yarn add @rails/webpacker

1

I ended up removing yarn. After solving the error presented in this question, I had another problem with yarn. I don't know if I should cross-post my solution, since it is the same. or just link to it

https://stackoverflow.com/a/58100373/683982

Hope this helps someone

Mathieu J.
  • 1,932
  • 19
  • 29
1

This means webpacker somehow is missing or uninstalled in your application.

rails webpacker:install

Should fix your problem.

Fungie
  • 113
  • 1
  • 7