首页 > 解决方案 > 在 Rails 中加载 webpack javascript 时出现 EOFError

问题描述

我的 Rails 6 项目中有一个文件app/javascript/packs/application.js。当我加载站点的本地开发版本时,它会尝试通过 http://localhost:4000/packs/js/application-ed3ae63cdf581cdb86b0.js 检索它(我在自定义端口上运行以避免与另一个应用程序发生冲突) ,但对 JS 文件的请求失败并返回 500:

Puma caught this error: end of file reached (EOFError)
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/2.6.0/net/protocol.rb:225:in `rbuf_fill'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/2.6.0/net/protocol.rb:191:in `readuntil'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/2.6.0/net/protocol.rb:201:in `readline'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/2.6.0/net/http/response.rb:40:in `read_status_line'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/2.6.0/net/http/response.rb:29:in `read_new'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-proxy-0.6.5/lib/net_http_hacked.rb:53:in `begin_request_hacked'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-proxy-0.6.5/lib/rack/http_streaming_response.rb:60:in `response'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-proxy-0.6.5/lib/rack/http_streaming_response.rb:29:in `headers'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-proxy-0.6.5/lib/rack/proxy.rb:120:in `perform_request'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/webpacker-4.3.0/lib/webpacker/dev_server_proxy.rb:21:in `perform_request'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-proxy-0.6.5/lib/rack/proxy.rb:57:in `call'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.3/lib/rails/engine.rb:527:in `call'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/puma-4.3.6/lib/puma/configuration.rb:228:in `call'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/puma-4.3.6/lib/puma/server.rb:713:in `handle_request'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/puma-4.3.6/lib/puma/server.rb:472:in `process_client'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/puma-4.3.6/lib/puma/server.rb:328:in `block in run'
/Users/sam/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/puma-4.3.6/lib/puma/thread_pool.rb:134:in `block in spawn_thread'

此文件在本地不存在:

$ ls public/packs/js/application-ed3ae63cdf581cdb86b0.js
ls: public/packs/js/application-ed3ae63cdf581cdb86b0.js: No such file or directory

但是具有另一个指纹的版本确实(并且非空):

$ ls -alh public/packs/js/application-*
-rw-r--r--  1 sam  staff   188K 24 Feb 17:58 public/packs/js/application-fdef50bf044896f1dc71.js
-rw-r--r--  1 sam  staff   212K 24 Feb 17:58 public/packs/js/application-fdef50bf044896f1dc71.js.map

我无法让我的网站请求正确的文件。在之后,我尝试过

rake assets:clean
rake assets:precompile
touch tmp/restart.txt

但无济于事。我还尝试了一个私人浏览器窗口,清除浏览器缓存和不同的浏览器。

我正在使用bin/server而不是bin/webpacker-dev-server并且没有更改我的webpacker.yml(它是在 init 上自动创建的,然后在我安装 React 时自动更新),但如果它有帮助,它看起来像这样:

# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_root_path: public
  public_output_path: packs
  cache_path: tmp/cache/webpacker
  check_yarn_integrity: false
  webpack_compile_output: true

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  # Extract and emit a css file
  extract_css: false

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

  extensions:
    - .jsx
    - .mjs
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

development:
  <<: *default
  compile: true

  # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
  check_yarn_integrity: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    pretty: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: '**/node_modules/**'


test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: true

我错过了什么?提前致谢!

标签: ruby-on-railsasset-pipeline

解决方案


看到您没有使用开发服务器,您会想到几件事,很可能在public/packs.

尝试删除该包文件夹并运行bin/server


推荐阅读