13

A brand new Rails 6.1.4.1 app fails to run bin/webpack-dev-server with the following error:

/rails_app/node_modules/webpack-cli/bin/utils/prompt-command.js:46
    return func(...args);
           ^

TypeError: Class constructor ServeCommand cannot be invoked without 'new'
    at runWhenInstalled (/rails_app/node_modules/webpack-cli/bin/utils/prompt-command.js:46:9)
    at promptForInstallation (/rails_app/node_modules/webpack-cli/bin/utils/prompt-command.js:140:10)
    at /rails_app/node_modules/webpack-cli/bin/cli.js:32:43
    at Object.<anonymous> (/rails_app/node_modules/webpack-cli/bin/cli.js:366:3)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
3
  • Unfortunately I cannot determine what the question is based on this post. Can you please update explaining the problem you are facing, an error or stack trace of some kind and some way to possibly reproduce it or at least what method or command is causing it? Sep 3, 2021 at 16:14
  • Posting as a comment, not an answer, because it's just a workaround ... I backed my versions down to "@rails/webpacker": "5.2.1", and "webpack-dev-server": "^3.11.2" to get back to a functional webpacker. Also related, for anyone hunting this down further: dev.to/arisa_dev/… Sep 3, 2021 at 18:03
  • James Chevalier: OK Let me know if you find out when I can upgrade from there This is really crazy Maybe I shouldn't be using the newest rails versions
    – Scott K
    Sep 3, 2021 at 23:48

4 Answers 4

19

This reported issue shows that webpack-dev-server is no longer compatible with Webpacker 5. This means that there are two fixes available to us:

  1. Force webpack-dev-server to stay on version 3

You can do this by updating your package.json file:

"webpack-dev-server": "~3"
  1. Upgrade to Webpacker 6

This version is not yet released and the upgrade path is not simple.

8
  • I found this workaround solution Uninstall system nodejs and install nvm and then use nvm to install nodejs again but now you can easily switch node versions in terminal sessions nvm add 16 and nvm use 16.0.0 Now since yarn add by default upgrades to the newest version run yarn add webpack/cli and then yarn add webpack-cli/serve Now you'll get error ./src not found mkdir src and then touch src/index.js After all that npx webpack serve works
    – Scott K
    Sep 4, 2021 at 1:00
  • I've since realized it's necessary to upgrade the rubygem webpacker to it's latest version after you've done all that
    – Scott K
    Sep 6, 2021 at 1:19
  • This can never be solved It's unpredictable behavior Stuck all over again on another app
    – Scott K
    Sep 6, 2021 at 16:34
  • There's also the matter of the rubygem webpacker and the npm module rails/webpacker and also the matter of there's webpack-cli component of webpack and if you install webpack-cli separately it seems to be creating a duplicate installation of webpack-cli with a different version
    – Scott K
    Sep 6, 2021 at 17:54
  • In my experiences with new and upgraded Rails apps, package.json sets "@rails/webpacker": "5.4.2" (which is ok and usable) and "webpack-dev-server": "^4.1.0" (which is not usable with Webpacker 5) and only changing ^4.1.0 to ~3 was enough to resolve the issue. Sep 7, 2021 at 14:36
1

I solved by downgrading webpack to 4.x.x because rails/webpacker 5.x.x is incompatible with webpack 5.x.x but works fine with 4.x.x I found this answer at this url on github https://github.com/webpack/webpack/issues/11649#issuecomment-779781768

4
  • Consider adding a link to where specifically you found the answer on GitHub.
    – Nol4635
    Sep 8, 2021 at 1:35
  • Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Sep 8, 2021 at 1:37
  • 2
    I found it's also necessary to run rails webpacker:install
    – Scott K
    Sep 10, 2021 at 0:54
  • Also there's an activestorage migration when upgrading from rails 6 to rails 6.1
    – Scott K
    Sep 14, 2021 at 2:45
0

You can watch this article at the end that explain how fix it: https://tundaapo.medium.com/the-web-as-a-means-of-communication-starting-with-rails-c10aa28b4fc2

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 13, 2021 at 10:34
0

As @Scott K suggested, I ran:

rails webpacker:install

and that fixed the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.