代码之家  ›  专栏  ›  技术社区  ›  Fred Willmore

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

  •  3
  • Fred Willmore  · 技术社区  · 7 年前

    我有一个最近升级到rails5.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\u modules”目录位于.gitinore中(我相信其中一个webpacker安装脚本将它放在了那里)。

    我是否需要取消忽略节点\模块,或者是否需要在配置文件中添加一些内容来告诉远程用户在哪里可以找到网页包?

    {
      "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"
      } 
    }
    

    # 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

    2 回复  |  直到 7 年前
        1
  •  0
  •   stephenmurdoch    7 年前

    我可以在我的本地机器上使用 yarn 1.10.1 . Heroku可能会使用旧版本的yarn,这可能会导致webpacker 3出现一些问题。

    # reinstall the node_modules
    yarn install
    
    # this is what heroku does to precompile your assets
    bundle exec rake assets:precompile
    

    这将更新你的yarn.lock文件。如果一切正常,那么再次尝试git提交和推送。

        2
  •  0
  •   Fred Willmore    7 年前

    我最终通过重建 /bin 目录使用以下内容:

    rails app:update:bin
    

    我推断我一定是破坏了那个目录中的某些东西。我是用 rails-jquery rails-react gems,然后使用webpacker安装脚本来安装jquery和react。我决定用 yarn add ... 添加jquery和rails,以便使用yarn更紧密、更一致地管理依赖关系。

    推荐文章