微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找不到命令“webpack”

如何解决找不到命令“webpack”

我已经为我的 Rails 6.1 项目安装了 webpack

    yarn add webpack
    yarn add -D webpack-cli
    yarn add @rails/webpacker@6.0.0-beta.6
    bin/rails webpacker:install

直到最近一切都运行良好,但现在出现错误

error Command "webpack" not found.

每当我执行以下任何一项时:

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

我假设它是由 webpack bin 命令“在幕后”运行的 yarn run webpack,因为我得到的所有 3 个命令的输出完全相同:

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

运行

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

两者都显示我安装了 webpacker 的 beta.6 版本

我有 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."
  }
}

和 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

我终生无法弄清楚现在缺少什么安装或配置步骤,尤其是因为它正在运行,我不确定发生了什么破坏它?

欢迎提出任何建议。

解决方法

令我惊讶的是,按照 https://makandracards.com/makandra/432947-how-to-fix-webpack-dev-server-not-found 上的建议

运行 yarn install --check-files 来修复这个错误,

我从以管理员身份(在 Windows 下)执行的命令提示符下使用 vagrant 重新启动了 VirtualBox VM,运行了命令,我在 webpack 和 webpack-dev-server 上遇到的所有问题都解决了。

我希望这对某人有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。