Search code examples
ruby-on-railswebpackyarnpkgwebpacker

Command "webpack" not found


I have installed webpack for my Rails 6.1 project with

    yarn add webpack
    yarn add -D webpack-cli
    yarn add @rails/[email protected]
    bin/rails webpacker:install

Up until recently everything was working perfectly but now I get the error

error Command "webpack" not found.

whenever I execute any of the following:

./bin/webpack
./bin/webpack-dev-server
yarn run webpack

I assuming that it is yarn run webpack which is being run "under the covers" by the webpack bin commands as I get exactly the same output for all 3 commands:

yarn run v1.22.5
error Command "webpack" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Running

bundle info webpacker 
yarn list --pattern @rails/webpacker

both show me that I have the beta.6 version of webpacker installed

I have package.json

# package.json

{
  "name": "ancestors",
  "private": true,
  "dependencies": {
    "@babel/core": "^7.12.10",
    "@babel/parser": "^7.13.13",
    "@babel/plugin-proposal-class-properties": "^7.13.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.13.8",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-transform-destructuring": "^7.13.0",
    "@babel/plugin-transform-regenerator": "^7.12.13",
    "@babel/plugin-transform-runtime": "^7.13.10",
    "@babel/preset-env": "^7.13.10",
    "@rails/actioncable": "^6.0.0",
    "@rails/activestorage": "^6.0.0",
    "@rails/ujs": "^6.0.0",
    "@rails/webpacker": "^6.0.0-beta.6",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.2.2",
    "babel-plugins": "^1.0.0",
    "coffee-loader": "^2.0.0",
    "coffeescript": "^2.5.1",
    "css-loader": "^5.1.3",
    "css-minimizer-webpack-plugin": "^1.3.0",
    "jquery": "^3.5.1",
    "jquery-blockui": "^2.7.0",
    "jquery-ui-dist": "^1.12.1",
    "mini-css-extract-plugin": "^1.3.9",
    "sass": "^1.32.8",
    "sass-loader": "^11.0.1",
    "style-loader": "^2.0.0",
    "turbolinks": "^5.2.0",
    "webpack": "^5.11.0"
  },
  "version": "0.1.0",
  "devDependencies": {
    "@webpack-cli/serve": "^1.3.0",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.2"
  },
  "engines": {
    "yarn": "1.22.5"
  },
  "scripts": {
    "heroku-prebuild": "echo This runs before Heroku installs dependencies.",
    "heroku-postbuild": "echo This runs after Heroku installs dependencies, but before Heroku prunes and caches dependencies",
    "heroku-cleanup": "echo This runs after Heroku prunes and caches dependencies."
  }
}

and webpacker.yml

# webpacker.yml

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

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

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

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

development:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    # Inject browserside javascript that required by both HMR and Live(full) reload
    inject_client: true
    # Hot Module Replacement updates modules while the application is running without a full reload
    hmr: false
    # Inline should be set to true if using HMR; it inserts a script to take care of live reloading
    inline: true
    # Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
    overlay: true
    # Should we use gzip compression?
    compress: true
    # Note that apps that do not check the host are vulnerable to DNS rebinding attacks
    disable_host_check: true
    # This option lets the browser open with your local IP
    use_local_ip: false
    # When enabled, nothing except the initial startup information will be written to the console.
    # This also means that errors or warnings from webpack are not visible.
    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

  # Cache manifest.json for performance
  cache_manifest: true

I cant for the life of me figure out what installation or configuration step is missing now especially as this was working and I am not sure what has happened to break it?

Any suggestions welcome.


Solution

  • To my great surprise, following a suggestion on https://makandracards.com/makandra/432947-how-to-fix-webpack-dev-server-not-found to

    Run yarn install --check-files to fix this error,

    I restarted the VirtualBox VM using vagrant from a Command prompt executed as Administrator (under Windows), ran the command, and all the issues I was having with webpack and webpack-dev-server are resolved.

    I hope that this helps someone.