如何将React(Frontend)和Go(BE)部署到heroku?

I'm trying to deploy a React + Go application to Heroku, but I'm struggling to understand what I'm doing wrong.

The application structure is as follows:

my-app
|
|__ build
|
|___ public
|    |-- manifest.json
|    |-- index.html
|    |-- favicon.ico
|
|___ server (Go)
|    |__ api
|    |__ db
|    |__ utils
|
|___ src (React)
|    |__ assets
|    |__ components
|        |__app
|        |  |-- app.js
|    |-- index.js
|
|-- .babelrc
|-- go.mod (Go modules)
|-- go.sum
|-- index.go
|-- package.json
|-- Procfile
|-- webpack.config
|-- webpack.development.js
|-- webpack.production.js

index.go

package main

import (
    "fmt"
    "net/http"
    "os"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir("./build")))
}

func GetPort() string {
    var port = os.Getenv("PORT")
    // Set a default port if there is nothing in the environment
    if port == "" {
        port = "4000"
        fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
    }
    return ":" + port
}

As can be seen, the backend is very simple which serves the static frontend pages in the build directory.

package.json

{
  "name": "fullstack-course",
  "main": "index.go",
  "scripts": {
    "client": "rm -rf build/ && webpack --env.mode production",
    "server": "rm -rf index && go build -o index",
    "start": "./index",
    "heroku-postbuild": "yarn run client && yarn run server"
  },
  "engines": {
    "node": "10.x",
    "npm": "6.9.x",
    "yarn": "1.x"
  },
  "dependencies": {...}
}

As for:

"client": "rm -rf build/ && webpack --env.mode production",

The client script clears the build, and then runs webpack.producton.js to bundle the frontend

As for:

"server": "rm -rf index && go build -o index",

The server removes the compiled file called index and builds the index again.

To run the server, we simply do "start": "./index",

Heroku Buildpacks

  1. heroku/nodejs
  2. heroku/go

Heroku log

When pushing this to Heroku, It fails the build with the following log

-----> Node.js app detected

-----> Creating runtime environment

       NPM_CONFIG_LOGLEVEL=error
       NODE_ENV=production
       NODE_MODULES_CACHE=false
       NODE_VERBOSE=false

-----> Installing binaries
       engines.node (package.json):  10.x
       engines.npm (package.json):   6.9.x
       engines.yarn (package.json):  1.x

       Resolving node version 10.x...
       Downloading and installing node 10.16.0...
       Bootstrapping npm 6.9.x (replacing 6.9.0)...
       npm 6.9.x installed
       Resolving yarn version 1.x...
       Downloading and installing yarn (1.16.0)...
       Installed yarn 1.16.0

-----> Restoring cache
       Caching has been disabled because NODE_MODULES_CACHE=false

-----> Installing dependencies
       Installing node modules (yarn.lock)
       yarn install v1.16.0
       [1/4] Resolving packages...
       [2/4] Fetching packages...
       info fsevents@1.2.9: The platform "linux" is incompatible with this module.
       info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
       [3/4] Linking dependencies...
       [4/4] Building fresh packages...
       Done in 21.23s.

-----> Build
       Running heroku-postbuild (yarn)
       yarn run v1.16.0
       $ yarn run client && yarn run server
       $ rm -rf build/ && webpack --env.mode production
       Hash: e3957a7f35b32ffdafe0
       Version: webpack 4.34.0
       Time: 6283ms
       Built at: 06/14/2019 4:43:46 PM
                                                Asset       Size  Chunks             Chunk Names
                                          favicon.ico   3.78 KiB          [emitted]  
                                           index.html  582 bytes          [emitted]  
                                        manifest.json  306 bytes          [emitted]  
                              resources-manifest.json  147 bytes          [emitted]  
                                    service-worker.js   21 bytes          [emitted]  
             static/css/main.b3bdf9a060b617f0a8a6.css   56 bytes       0  [emitted]  main
               static/js/main.2404dde5082f7f65e664.js   3.21 KiB       0  [emitted]  main
       static/js/vendors~main.64517854689ee0e9248d.js    135 KiB       1  [emitted]  vendors~main
       Entrypoint main = static/js/vendors~main.64517854689ee0e9248d.js static/css/main.b3bdf9a060b617f0a8a6.css static/js/main.2404dde5082f7f65e664.js
        [8] (webpack)/buildin/global.js 472 bytes {1} [built]
       [12] ./node_modules/react-redux/es/index.js + 18 modules 29.8 KiB {1} [built]
            |    19 modules
       [20] (webpack)/buildin/harmony-module.js 573 bytes {1} [built]
       [22] ./src/index.js + 5 modules 5.26 KiB {0} [built]
            | ./src/index.js 483 bytes [built]
            | ./src/store.js 1.29 KiB [built]
            | ./src/reducers/index.js 274 bytes [built]
            | ./src/reducers/initial.js 607 bytes [built]
            |     + 2 hidden modules
           + 20 hidden modules
       Child html-webpack-plugin for "index.html":
            1 asset
           Entrypoint undefined = index.html
           [0] ./node_modules/html-webpack-plugin/lib/loader.js!./public/index.html 1.9 KiB {0} [built]
           [2] (webpack)/buildin/global.js 472 bytes {0} [built]
           [3] (webpack)/buildin/module.js 497 bytes {0} [built]
               + 1 hidden module
       Child mini-css-extract-plugin node_modules/css-loader/dist/cjs.js!src/components/app/App.css:
           Entrypoint mini-css-extract-plugin = *
              2 modules
       $ rm -rf index && go build -o index
/bin/sh: 1: go: not found
error Command failed with exit code 127.
       info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 127.
       info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
-----> Build failed

       We're sorry this build is failing! You can troubleshoot common issues here:
       https://devcenter.heroku.com/articles/troubleshooting-node-deploys

       If you're stuck, please submit a ticket so we can help:
       https://help.heroku.com/

       Love,
       Heroku

 !     Push rejected, failed to compile Node.js app.
 !     Push failed

I have no idea why Heroku can't run the go command. Please, can someone help?

</div>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问