使用 webpack 部署到 Heroku 时预编译资产时出错

Error precompiling assets while deploying to Heroku with webpack

我有一个项目,最近升级到 rails 5.2,我正在使用 webpacker gem。尝试部署到 Heroku 时出现以下错误:

...
remote:        Webpacker is installed  
remote:        Using /tmp/build_b969a2366f45a65a0c09b6aaa4b24667/config/webpacker.yml file for setting up webpack paths
remote:        Compiling…
remote:        Compilation failed:
remote:        /tmp/build_b969a2366f45a65a0c09b6aaa4b24667/vendor/bundle/ruby/2.5.0/gems/webpacker-3.5.5/lib/webpacker/webpack_runner.rb:11:in `exec': No such file or directory - /tmp/build_b969a2366f45a65a0c09b6aaa4b24667/node_modules/.bin/webpack (Errno::ENOENT)
remote:         from /tmp/build_b969a2366f45a65a0c09b6aaa4b24667/vendor/bundle/ruby/2.5.0/gems/webpacker-3.5.5/lib/webpacker/webpack_runner.rb:11:in `block in run'
remote:         from /tmp/build_b969a2366f45a65a0c09b6aaa4b24667/vendor/bundle/ruby/2.5.0/gems/webpacker-3.5.5/lib/webpacker/webpack_runner.rb:10:in `chdir'
remote:         from /tmp/build_b969a2366f45a65a0c09b6aaa4b24667/vendor/bundle/ruby/2.5.0/gems/webpacker-3.5.5/lib/webpacker/webpack_runner.rb:10:in `run'
remote:         from /tmp/build_b969a2366f45a65a0c09b6aaa4b24667/vendor/bundle/ruby/2.5.0/gems/webpacker-3.5.5/lib/webpacker/runner.rb:6:in `run'
remote:         from ./bin/webpack:15:in `<main>'
...

本地我的 'node_modules' 目录在 .gitignore 中(我相信其中一个 webpacker 安装脚本放在那里)。

我需要取消忽略 node_modules,还是我需要在配置文件中添加一些东西来告诉远程在哪里可以找到 webpack?

这是我的 package.json:

{
  "dependencies": {
    "@rails/webpacker": "3.5.5",
    "babel-preset-react": "^6.24.1",
    "d3": "^5.7.0",
    "jquery": "^3.3.1",
    "prop-types": "^15.6.2",
    "react": "^16.5.2",
    "react-addons-transition-group": "^15.6.2",
    "react-dom": "^16.5.2",
    "react-transition-group": "^2.4.0",
    "react_ujs": "^2.4.4"
  },
  "devDependencies": {
    "webpack-dev-server": "2.11.2"
  },
  "engines": {
    "yarn": "1.10.1"
  } 
}

我注意到 heroku 的输出提到使用 config/webpacker.yml 设置 webpacker 路径,但我在该文件中找不到任何关于在 node_modules 中寻找 webpack 的提及 - 这是webpacker.yml 的文本:

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

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

  # 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

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

development:
  <<: *default
  compile: 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
    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

可以找到整个项目on github

我能够使用 yarn 1.10.1 在我的本地计算机上预编译您的资产。 Heroku 可能使用旧版本的 yarn,这可能会导致 webpacker 3 出现一些问题。

因此请尝试升级到最新版本的 yarn,然后 运行 在您的本地计算机上执行以下操作:

# reinstall the node_modules
yarn install

# this is what heroku does to precompile your assets
bundle exec rake assets:precompile

这应该会更新您的 yarn.lock 文件。如果一切正常,则尝试 git 提交并再次推送。

希望这会对其进行排序。祝你好运。

我最终通过使用以下命令重建 /bin 目录解决了这个问题:

rails app:update:bin

我推断我一定是损坏了那个目录中的某些东西。我已经使用 rails-jqueryrails-react gem 启动了项目,然后使用 webpacker 安装脚本安装 jquery 并做出反应。我决定使用 yarn add ... 添加 jquery 和 rails 以便我可以更紧密地管理我的依赖关系,并与 yarn 保持一致。

我有一大堆问题,只是 开始 以上错误。

它让我踏上了一段错综复杂的网络之旅。我希望这些想法可以帮助其他人。

这是我所做的:

1.

确保在 heroku 上设置了 node buildpack(我还没搞清楚为什么这在过去不是必需的,现在突然是必需的,但那是另一次了)

heroku buildpacks:add --index 1 heroku/nodejs

2.

注意我不确定这一步是否真的有必要,因为我在一切正常后删除了环境变量,没有它们它仍然有效。所以我怀疑你可以跳过这一步。但请注意,其他人有 reported it worked for them,因此值得一试。

在 heroku 上设置这些环境变量。

heroku config:set NPM_CONFIG_PRODUCTION=false YARN_PRODUCTION=false
# Note: you can unset them with 
# heroku config:unset NPM_CONFIG_PRODUCTION YARN_PRODUCTION
# If you decide you don't want them

3.

有进展,但现在我收到了这条错误消息

configuration.node should be one of these: false | object { __dirname?, __filename?, global? }

because:

Rails' webpacker 5.x.x is only compatible with webpack 4.x.x (https://github.com/rails/webpacker/tree/5-x-stable). You might want to downgrade to webpack 4 :)

所以我只是 运行 yarn add webpack@4.46.0 添加 most recent 4.x.x version of webpack.


然后,终于,推送到 heroku 成功了!