Rails:Vanilla Rails 6.0 error Command“webpack”not found

ao218c7q  于 2023-06-06  发布在  Webpack
关注(0)|答案(8)|浏览(319)

系统:

Ruby:2.6.3p62(rvm)
Rails:6.0
操作系统:macOS 10.14.6

设置

一个全新的Rails 6.0应用程序:

$ 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

当我浏览到“http://localhost:3000/page/index”时,系统会出现以下错误:

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

我该怎么解决这个问题?它说error Command "webpack" not found.,但Rails不应该自动安装所需的一切吗?

xxe27gdn

xxe27gdn1#

我也面临着同样的问题。并按照@AFOC的回答尝试运行bundle exec rails webpacker:install。然后我意识到我的node版本与rails/webpacker不兼容。然后使用节点12.x。这很有效。
我也我重试删除整个应用程序,再次创建应用程序,没有再次面临的问题,它的工作第一次。所以在我的例子中,它是节点版本。

bxfogqkk

bxfogqkk2#

问题来自本地计算机上的节点版本。当你创建你的rails应用程序时,你可能会弹出这样一个错误:

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.

在我的例子中,我的计算机上安装了node的9.4.0版本,这是get-caller-file包不想使用的版本。
要解决此问题,请首先确认您的node版本,以确保它不是9.x.x:

node -v

更新节点(我使用nvm,但node网站上有other ways on GitHub)。

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

再次检查版本。如果它是12.10或更高,你应该都设置。
去启动另一个全新的Rails项目,它应该会工作得很好。

hl0ma9xz

hl0ma9xz3#

当我尝试在Ubuntu 20.04中设置一个新的Rails 6应用程序时,我也遇到了同样的挑战。
当我启动rails服务器时,我得到错误:

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

我是这样修复的

对我来说,这个问题是由于糟糕的互联网连接,这不允许完全安装webpacker及其所有依赖项。所以每次我运行命令:

bundle exec rails webpacker:install

我得到这个输出:

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...

我只需要移动到一个互联网连接更好的区域,然后运行命令:

bundle exec rails webpacker:install

rails webpacker:install

这一次,它成功运行并安装了webpacker及其所有依赖项,并给出了最终输出:

[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.
eulz3vhy

eulz3vhy4#

我在一个新的6.0安装上也遇到了这个问题,就我所知,它与node或yarn无关。我注意到package.json中没有提到webpack,所以我运行yarn add webpack并再次尝试。我得到了以下消息,说它编译了,但页面仍然没有加载:

[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.

所以我运行了yarn add webpack-cli。然后我得到了Error: Cannot find module '@rails/webpacker'
所以我删除了node_modules文件夹,然后运行bundle exec rails webpacker:install。在那之后它终于起作用了。
编辑:如果你在Windows中的Vagrant VM上运行,请确保以管理员身份启动它,否则通过yarn添加依赖关系可能会失败。

mbyulnm0

mbyulnm05#

试着做:

bundle exec rails webpacker:install

如果仍然不工作,请尝试:

npm install
yarn install check--files
qvtsj1bj

qvtsj1bj6#

检查已安装的Yarn卷装。在我的例子中,yarn版本与rails不兼容。运行以下命令:

yarn add @rails/webpack

biswetbf

biswetbf7#

最后我把毛线拿掉了。在解决了这个问题中出现的错误后,我遇到了另一个关于Yarn的问题。我不知道我是否应该交叉张贴我的解决方案,因为它是相同的。或者只是链接到它
https://stackoverflow.com/a/58100373/683982
希望这对某人有帮助

bqucvtff

bqucvtff8#

这意味着webpacker不知何故在您的应用程序中丢失或卸载。

rails webpacker:install

应该能解决你的问题

相关问题