【问题标题】:Rails: Vanilla Rails 6.0 error Command "webpack" not foundRails:Vanilla Rails 6.0 错误命令“webpack”未找到
【发布时间】:2020-01-23 16:48:11
【问题描述】:

系统:

Ruby:2.6.3p62 (rvm)
导轨: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 不应该自动安装所有需要的东西吗?

【问题讨论】:

  • 对于今天路过的任何人,请注意:Webpacker 似乎不太喜欢 Node v16。试试 v14

标签: ruby-on-rails webpack ruby-on-rails-6


【解决方案1】:

尝试做:

bundle exec rails webpacker:install

如果它仍然不起作用,请尝试:

 npm install
yarn install check--files 

【讨论】:

    【解决方案2】:

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

    rails webpacker:install
    

    应该可以解决您的问题。

    【讨论】:

      【解决方案3】:

      尝试在 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.
      

      就是这样。

      我希望这会有所帮助

      【讨论】:

      • bundle exec rails webpacker:install ./bin/webpack-dev-server 它工作了
      • 完美答案:rails webpacker:install 就这么简单,谢谢!
      【解决方案4】:

      我遇到了同样的问题。并尝试按照@AFOC 的回答建议运行bundle exec rails webpacker:install。然后我意识到我的节点版本与 rails/webpacker 不兼容。然后使用节点 12.x。它有效。

      我也通过删除整个应用程序重试,再次创建应用程序,没有再次遇到问题,它第一次工作。所以就我而言,它是节点版本。

      【讨论】:

      • 这个命令本身实际上让我走上了这条路。我仍然对 webpack 配置有问题,但这让我摆脱了这个问题。谢谢!
      【解决方案5】:

      我在全新的 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 添加依赖项可能会失败。

      【讨论】:

        【解决方案6】:

        我最终去掉了纱线。 在解决了这个问题中出现的错误之后,我遇到了另一个纱线问题。 我不知道我是否应该交叉发布我的解决方案,因为它是相同的。或者只是链接到它

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

        希望这对某人有所帮助

        【讨论】:

          【解决方案7】:

          编辑:

          这是我的第一篇文章,如果我的发帖约定有误,请见谅。

          问题来自本地计算机上的节点版本。当您创建 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.
          

          在我的情况下,我的计算机上安装了 9.4.0 版本的节点,这是 get-caller-file 包不想使用的一个版本。

          要解决此问题,请先确认您的节点版本以确保它不是 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 项目,它应该可以正常工作。

          祝你好运!

          【讨论】:

          • node v10.15.2 怎么样?应该支持吗?我们如何找出什么版本的 webpacker 需要什么版本的 node?等等?
          • 我在 ubuntu 上运行 snap install node --edge --classic 我现在有节点 13.0.0-nightly 而不是 10.15,希望效果更好
          • 可能也很重要,npm@6.10.3 与这一切的兼容性如何?
          • 我得到的关于节点版本的唯一迹象是我创建应用程序或特别尝试纱线添加 webpacker 时的问题。
          猜你喜欢
          • 2017-12-04
          • 1970-01-01
          • 1970-01-01
          • 2017-05-26
          • 2022-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多