職長が Windows 10 の問題を開始

概要

Windows 10 x64 の Rails プロジェクト内で webpack スクリプトを実行すると、正しく動作しません。

Windowsの場合は実行前に環境設定が必要なようです

フォアマンがエラー以下で投げ始めた

$ foreman start

Traceback (most recent call last):
        17: from C:/Ruby27/bin/foreman:23:in `<main>'
        16: from C:/Ruby27/bin/foreman:23:in `load'
        15: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/bin/foreman:7:in `<top (required)>'
        14: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/vendor/thor/lib/thor/base.rb:444:in `start'
        13: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/vendor/thor/lib/thor.rb:369:in `dispatch'
        12: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
        11: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/vendor/thor/lib/thor/command.rb:27:in `run'
        10: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/cli.rb:42:in `start'
         9: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/engine.rb:57:in `start'
         8: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/engine.rb:363:in `spawn_processes'
         7: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/engine.rb:363:in `each'
         6: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/engine.rb:364:in `block in spawn_processes'
         5: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/engine.rb:364:in `upto'
         4: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/engine.rb:367:in `block (2 levels) in spawn_processes'
         3: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/process.rb:53:in `run'
         2: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/process.rb:53:in `chdir'
         1: from C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/process.rb:54:in `block in run'
C:/Ruby27/lib/ruby/gems/2.7.0/gems/foreman-0.87.2/lib/foreman/process.rb:54:in `spawn': Exec format error - bin/webpack-dev-server (Errno::ENOEXEC)

GemFile

ruby "2.7.2"
gem "rails", "~> 6.0"
gem "webpacker", "< 6"

group :development do
  gem "foreman", require: false
end

パッケージ.json

{
  "name": "demo",
  "private": true,
  "engines": {
    "node": ">=10.15.3",
    "npm": ">=6",
    "yarn": ">=1.15.2"
  },
  "dependencies": {
    "@rails/webpacker": "5.4.3",
    "webpack": "4.46.0",
    "webpack-cli": "3.3.12"
  },
  "version": "0.1.0",
  "devDependencies": {
    "webpack-dev-server": "3"
  },
}

編集1:

ただし、別の端末で bin/webpack-dev-server を実行すると、正しくコンパイルされます。

$ bin/webpack-dev-server

i 「wds」: Project is running at http://localhost:3035/
i 「wds」: webpack output is served from /packs/
i 「wds」: Content not from webpack is served from C:\Users\DELL\path\project\plate\public\packs
i 「wds」: 404s will fallback to /index.html
i 「wdm」: Hash: 3f9f73c5cf41d5c81ee8
Version: webpack 4.46.0
Time: 4740ms
Built at: 12/01/2022 14:01:39
                                     Asset       Size       Chunks                         Chunk Names
    js/application-f0238c055e7bba05cd93.js    515 KiB  application  [emitted] [immutable]  application
js/application-f0238c055e7bba05cd93.js.map    581 KiB  application  [emitted] [dev]        application
                             manifest.json  364 bytes               [emitted]
i 「wdm」: Compiled successfully.

編集2:

次の回答に従って、Gemfileからforemanを削除し、gemコマンドを使用して再インストールしてみました

Gemfile

group :development do
  # gem "foreman", require: false
end
$ gem uninstall foreman
$ gem install foreman

Bundle exec Rails webpacker:install を試してみましたが、再び foreman start が同じエラーをスローします。 bin/webpack-dev-server が PORT 上で config/webpack ファイルとして適切にコンパイルされている間。

$ bundle exec rails webpacker:install

  Please add the following to your Gemfile to avoid polling for changes:
    gem 'wdm', '>= 0.1.0' if Gem.win_platform?
  Please add the following to your Gemfile to avoid polling for changes:
    gem 'wdm', '>= 0.1.0' if Gem.win_platform?
  Please add the following to your Gemfile to avoid polling for changes:
    gem 'wdm', '>= 0.1.0' if Gem.win_platform?
   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:/Users/DELL/Documents/project/plate/vendor/cache/ruby/2.7.0/gems/webpacker-5.4.3/lib/install/binstubs.rb
  Copying binstubs
       exist    bin
   identical    bin/webpack
   identical    bin/webpack-dev-server
File unchanged! The supplied flag value not found!  .gitignore
Installing all JavaScript dependencies [5.4.3]
         run  yarn add @rails/[email protected] from "."
yarn add v1.22.15
[1/5] Validating package.json...
[2/5] Resolving packages...
[3/5] Fetching packages...
info [email protected]: The platform "win32" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
info [email protected]: The platform "win32" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[4/5] Linking dependencies...
[5/5] Building fresh packages...
warning Your current version of Yarn is out of date. The latest version is "1.22.17", while you're on "1.22.15".
info To upgrade, download the latest installer at "https://yarnpkg.com/latest.msi".
success Saved 0 new dependencies.
Done in 8.92s.
Installing webpack and webpack-cli as direct dependencies
         run  yarn add webpack@^4.46.0 webpack-cli@^3.3.12 from "."
yarn add v1.22.15
[1/5] Validating package.json...
[2/5] Resolving packages...
[3/5] Fetching packages...
info [email protected]: The platform "win32" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
info [email protected]: The platform "win32" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[4/5] Linking dependencies...
[5/5] Building fresh packages...
success Saved 0 new dependencies.
Done in 6.82s.
Installing dev server for live reloading
         run  yarn add --dev webpack-dev-server@^3 from "."
yarn add v1.22.15
[1/5] Validating package.json...
[2/5] Resolving packages...
[3/5] Fetching packages...
info [email protected]: The platform "win32" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
info [email protected]: The platform "win32" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[4/5] Linking dependencies...
[5/5] Building fresh packages...
success Saved 0 new dependencies.
Done in 7.33s.
Webpacker successfully installed 🎉 🍰

解決策

「Procfile」という名前の新しいファイルをプロジェクトのルートに追加します。 Procfileに以下を挿入します

web: bundle exec rails server -p $PORT

これで、コマンドを使用してサーバーを実行できるようになります

heroku local

http://localhost:5000 にあります。