Webpacker 找不到应用程序

Posted

技术标签:

【中文标题】Webpacker 找不到应用程序【英文标题】:Webpacker can't find application 【发布时间】:2020-02-04 18:50:29 【问题描述】:

我正在根据“Rails 入门”创建一个新的 Rails 应用程序。应用程序在步骤 4.1 中正常启动。但是当我创建我的第一个控制器时,我遇到了 webpack 错误。

我是 webpack 的新手,我的所有文档都是红色的,但我不明白有什么问题或遗漏...

Windows 10 1903 红宝石 2.6 导轨 6.0.0 nodeJS 12.11.0
rails webpacker:install
   identical  config/webpacker.yml
Copying webpack core config
       exist  config/webpack
   identical  config/webpack/development.js
   identical  config/webpack/environment.js
   identical  config/webpack/production.js
   identical  config/webpack/test.js
Copying postcss.config.js to app root directory
   identical  postcss.config.js
Copying babel.config.js to app root directory
   identical  babel.config.js
Copying .browserslistrc to app root directory
   identical  .browserslistrc
The javascript app source directory already exists
       apply  C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/webpacker-4.0.7/lib/install/binstubs.rb
  Copying binstubs
       exist    bin
   identical    bin/webpack
   identical    bin/webpack-dev-server
      append  .gitignore
Installing all JavaScript dependencies [4.0.7]
         run  yarn add @rails/webpacker from "."
yarn add v1.17.3
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.9: The platform "win32" is incompatible with this module.
info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > webpack-dev-server@3.8.2" has unmet peer dependency "webpack@^4.0.0".
warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has unmet peer dependency "webpack@^4.0.0".
[4/4] Building fresh packages...
success Saved 1 new dependency.
info Direct dependencies
└─ @rails/webpacker@4.0.7
info All dependencies
└─ @rails/webpacker@4.0.7
Done in 4.48s.
Installing dev server for live reloading
         run  yarn add --dev webpack-dev-server from "."
yarn add v1.17.3
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.9: The platform "win32" is incompatible with this module.
info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has unmet peer dependency "webpack@^4.0.0".
warning " > webpack-dev-server@3.8.2" has unmet peer dependency "webpack@^4.0.0".
[4/4] Building fresh packages...
success Saved 1 new dependency.
info Direct dependencies
└─ webpack-dev-server@3.8.2
info All dependencies
└─ webpack-dev-server@3.8.2
Done in 4.92s.
Webpacker successfully installed �????????????

Webpacker::Manifest::MissingEntryError in Welcome#index

Showing C:/Users/[...]/site_test/app/views/layouts/application.html.erb where line #9 raised:

Webpacker can't find application in C:/Users/[...]/site_test/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:



Extracted source (around line #9):

7
8      <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
9      <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
10 </head>
11 <body>
12

【问题讨论】:

【参考方案1】:

这个错误似乎是关键。

Your manifest contains:


尝试在您的项目目录中简单地运行yarn 命令。这应该会导致创建一个 manifest.json 文件 - 除其他外。

如果这不起作用,我在 github 上找到了 this issue,我正从那里 逐字 背诵以下可能对您有所帮助的命令。这将重置所有构建的资产和 node_modules。

rm -rf node_modules
rails assets:clobber
yarn
rails assets:precompile

查看该链接了解更多信息。

【讨论】:

bundle exec rails webpacker:clobber 从项目中删除 public/packs 文件夹,其中包含 public/packs/manifest.json 文件,yarn 将其恢复。对于任何有同样问题的人,最好使用clean-webpack-plugin【参考方案2】:

感谢您的帮助。不幸的是,没有任何效果。

rails assets:clobber 在删除 node_modules 文件夹后立即返回 “错误找不到完整性文件”。 (在 yarn install --check-files 之后工作)。

rails assets:precompile返回

"C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/webpacker-4.0.7/lib/webpacker/webpack_runner.rb:23:in `exec': No such file or directory - C:\Users\[...]\site_test\node_modules\.bin/webpack (Errno::ENOENT)"

但我发现了问题所在:我电脑中的位置! 可能是名字问题...

【讨论】:

嗨,这件事昨天发生在我身上。我按照yarnpkg.com/getting-started/install 上的说明进行操作【参考方案3】:

我在 Rails 5.2 应用程序中收到此错误消息,我根据其 README 中的说明添加了 Webpacker。

在尝试了其他一些事情之后,我尝试启动 webpacker-dev-server(即使它不应该是必要的)只是为了看看这是否会显示一个更有帮助的错误。它确实做到了:

$ ./bin/webpack-dev-server
warning package.json: No license field
Another program is running on port 3035. Set a new port in  for dev_server

然后我切换到另一个我正在运行的项目,它也使用 Webpacker,并关闭了它的 webpack-dev-server(只需 ctrl+c:ed 正在运行的服务器)。现在,当我在这个新项目中重新加载页面时,我不再收到错误消息。即使没有我手动运行 webpack-dev-server。

您可以在config/webpacker.ymldev_server: 部分更改此端口。如有必要,不要忘记在config/initializers/content_security_policy.rb 中更新它。

【讨论】:

【参考方案4】:

运行命令

rails webpacker:install

这解决了我的问题。

【讨论】:

【参考方案5】:

我遇到了同样的错误 - 为我修复:我在我的 .zshrc 文件中设置了一个 NODE_ENV 环境变量,导致 Webpack 构建使用错误的环境配置,取消设置该环境变量为我修复了它( unset NODE_ENV)

【讨论】:

我的.zshrc 中没有那个变量,我仍然遇到这个问题。 该变量可能在其他地方设置 - 尝试运行 unset NODE_ENV 以确保它未设置 - 澄清答案。【参考方案6】:

就像上面萨尔曼的回答一样,命令

$ rails webpacker:install

$ bundle exec rails webpacker:install

解决了这个问题。这对我来说很奇怪,因为 rails 不会自动安装 webpacker,但无论如何都会在默认布局中包含javascript_pack_tag。但是,如果 yarn 抱怨您的节点版本,请确保安装正确的版本并切换到它,然后再次运行上述命令。这个简单的事情我花了一个小时才弄清楚。例如:

$ nvm install 13.7
$ nvm use 13.7
$ rails webpacker:install

【讨论】:

你救了我的命。谢谢!我认为yarn install命令也是必需的,我不记得了 很好的答案 - 这对我有用!一个小区别是我进入 Node.js 主页查看最新版本是什么,然后从那里安装它。然后我运行rails webpacker:install,然后一切正常。【参考方案7】:

从某处溜走。空清单。错误:Welcome#index 中的 Webpacker::Manifest::MissingEntryError 我把它卡在了 public/packs/manifest.json 现在她会说法语。


  "application.js": "/packs/js/application-9afcbb5693aa87623e69.js",
  "application.js.map": "/packs/js/application-9afcbb5693aa87623e69.js.map",
  "entrypoints": 
    "application": 
      "js": [
        "/packs/js/application-9afcbb5693aa87623e69.js"
      ],
      "js.map": [
        "/packs/js/application-9afcbb5693aa87623e69.js.map"
      ]
    
  

【讨论】:

【参考方案8】:

为我解决这个问题的方法是将 nodejs 降级到其稳定版本(我不小心下载了最新版本),然后再次创建新的 rails 应用程序。

【讨论】:

【参考方案9】:

这是我为未来 Rails 6.1+ 和 Ruby 2.7.1+ 用户提供的解决方案。

对我来说,这是因为我的 webpacker gem 在Gemfile 中设置为 4.0 版本。一旦我将它提升到 5.0 版本并运行bundle update,这个错误就消失了。看起来 webpacker 在运行时可能一直在默默地失败,并且从未创建过 public/packs 文件夹。将我的 Gemfile 更改为最新版本的 Webpacker 为我解决了这个错误。

Gemfile 更改

DELETED gem 'webpacker',    '~> 4.0.7'
ADDED   gem 'webpacker',    '~> 5.0'

【讨论】:

这在 2021 年 6 月对我有用。Rails 6.0.3【参考方案10】:

在执行以下步骤之前,我遇到此错误的时间比我愿意提及的要长:

首先,webpacker 在我的Gemfile 中默认被锁定为 4.0 版本。我将它升级到 5.0 并运行 bundle update

然后:

rm -rf node_modules
rails assets:clobber
yarn
rails assets:precompile

预编译资产时出现新错误:

yarn install v1.22.4
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.36s.
I, [2021-04-09T11:04:33.781374 #16062]  INFO -- : Writing /app/public/assets/manifest-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.js
I, [2021-04-09T11:04:33.781915 #16062]  INFO -- : Writing /app/public/assets/manifest-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.js.gz
Compiling...
Compilation failed:
[webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.

所以我编辑了config/webpack/environment.js 文件来阅读:

const  environment  = require('@rails/webpacker')

const customConfig = 
  resolve: 
    fallback: 
      dgram: false,
      fs: false,
      net: false,
      tls: false,
      child_process: false
    
  
;

environment.config.delete('node.dgram')
environment.config.delete('node.fs')
environment.config.delete('node.net')
environment.config.delete('node.tls')
environment.config.delete('node.child_process')

environment.config.merge(customConfig);

module.exports = environment

只有这样才能加载javascript_load_tag。它可能不适用于所有人,但希望在相同情况下可以节省其他人的时间。

【讨论】:

【参考方案11】:

这发生在我最近在标准 docker ruby​​ 容器中运行 Rails 的身上。原来我需要添加:

RUN apt-get update & apt-get install nodejs npm -y & npm install --global yarn

到我的 docker 文件,以便通过初始 rails smoketest 屏幕渲染任何内容。

【讨论】:

【参考方案12】:

我遇到了同样的问题并正在运行: rails webpacker:compile 已修复

来源:https://github.com/rails/webpacker/issues/2825#issuecomment-764888322

【讨论】:

【参考方案13】:

我不知道是不是太晚了,但我为此奋斗了一段时间,经过无数小时的研究,唯一的解决方案是注释第 9 行,即 JavaScript 标记,它可以工作并允许你的代码编译,但你不会能够在您的应用程序(使用 javascript)中运行方法,让您回到原点。要解决此问题,请转到

“C:/Users/[...]/site_test/public”

然后查找 packs 文件夹。如果文件夹不存在,那就是问题所在。

每当您的应用程序运行时,它都会编译并查找与 application.html.erb 文件关联的所有可用文件,因此您有一个样式表链接标签 (application.css) 和一个 javascript 链接标签 (application.js)是由于缺少 application.js。

解决方案: 要解决此问题,请在 ("C:/Users/[...]/site_test/public/") 中创建一个 packs 文件夹 您的公用文件夹和 packs 文件夹内创建一个 manifest.json 文件。即

你的应用名称/public/packs/manifest.json

完成所有这些后,在 manifest.json 文件中添加以下代码。


  "application.js": "/packs/js/application-e421b4aa3f716bebdab1.js",
  "application.js.map": "/packs/js/application-e421b4aa3f716bebdab1.js.map",
  "entrypoints": 
    "application": 
      "js": [
        "/packs/js/application-e421b4aa3f716bebdab1.js"
      ],
      "js.map": [
        "/packs/js/application-e421b4aa3f716bebdab1.js.map"
      ]
    
  

希望这样可以解决问题

【讨论】:

【参考方案14】:

运行yarn add @rails/webpacker 然后rails webpacker:install 为我解决了这个问题。

【讨论】:

【参考方案15】:

这里没有什么可以为我解决这个问题。所以我删除了该应用程序并再次尝试,之后它就可以工作了。好像有安装命令。如果你没有正确理解它,它就不起作用。

【讨论】:

【参考方案16】:

更新节点 - >=12

我的版本 - 16.13.1

从 git 使用 webpacker -

gem 'webpacker', git: 'https://github.com/rails/webpacker.git'

将 application.js 从 app/javascript/packs 移动到 app/javascript/

停止服务器,启动 这很奇怪。但这对我有用

【讨论】:

更新:与 webpacker 中的解析路径有关。

以上是关于Webpacker 找不到应用程序的主要内容,如果未能解决你的问题,请参考以下文章

Rails:Webpacker 4.2 在 /app/public/packs/manifest.json heroku 中找不到应用程序

Webpacker 在 /app/public/packs/manifest.json 中找不到 application.css

stylesheet_pack_tag 在带有 webpacker gem 的 rails 5.1 中找不到 app/javascript/src/application.css

Rails 6 webpacker找不到已安装模块的模块

Webpacker 找不到 https://js.stripe.com/v3/.js

如何在 rails 6/webpacker 中引用 scss 文件路径?