Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)

前端 未结 24 1927
梦如初夏
梦如初夏 2020-11-22 05:42

I have my first node.js app (runs fine locally) - but I am unable to deploy it via heroku (first time w/ heroku as well). The code is below. SO doesn\'t let me write so much

相关标签:
24条回答
  • 2020-11-22 06:29

    I Use ReactJs, If you want upload to heroku add this in your webpack.config.js

    Because if not add you will have error

    Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

    //webpack.config.js add code like that
    
    const HtmlWebPackPlugin = require("html-webpack-plugin");
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    var server_port = process.env.YOUR_PORT || process.env.PORT || 5000;
    var server_host = process.env.YOUR_HOST || "0.0.0.0";
    
    module.exports = {
      module: {
        rules: [
          {
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
              loader: "babel-loader"
            }
          },
          {
            test: /\.css$/,
            use: [MiniCssExtractPlugin.loader, "css-loader"]
          }
        ]
      },
      devServer: {
        disableHostCheck: true,
        contentBase: "./ dist",
        compress: true,
        inline: true,
        port: server_port,
        host: server_host
    
      },
      plugins: [
        new HtmlWebPackPlugin({
          template: "./src/index.html",
          filename: "index.html"
        }),
        new MiniCssExtractPlugin({
          filename: "[name].css",
          chunkFilename: "[id].css"
        })
      ]
    };
    
    0 讨论(0)
  • 2020-11-22 06:29

    I had same issue but with express and apollo-server. The solution from here:

    The only special consideration that needs to be made is to allow heroku to choose the port that the server is deployed to. Otherwise, there may be errors, such as a request timeout.

    To configure apollo-server to use a port defined by Heroku at runtime, the listen function in your setup file can be called with a port defined by the PORT environment variable:

    > server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => { 
    > console.log(`Server ready at ${url}`); });
    
    0 讨论(0)
  • 2020-11-22 06:29

    In my case, neither the port nor the host was the problem. The index.js was divided into 2 files. server.js:

    //server.js
    const express = require('express')
    const path = require('path')
    
    const app = express()
    
    app.use(express.static(path.resolve(__dirname, 'public')));
    // and all the other stuff
    module.exports = app
    
    //app.js
    const app = require('./server');
    const port = process.env.PORT || 3000;
    app.listen(port, '0.0.0.0', () => {
        console.log('Server is running s on port: ' + port)
    });
    

    from package.json we ran node app.js.

    Apparently that was the problem. Once I combined the two into one file, the Heroku app deployed as expected.

    0 讨论(0)
  • 2020-11-22 06:30

    It's worth mentioning that if your code doesn't specify a port, then it shouldn't be a web process and probably should be a worker process instead.

    So, change your Procfile to read (with your specific command filled in):

    worker: YOUR_COMMAND
    

    and then also run on CLI:

    heroku scale worker=1
    
    0 讨论(0)
  • 2020-11-22 06:33

    If, like me, you're configuring Heroku to run a script from your package.json file on deploy, make sure you haven't hard-coded the value of PORT in that script! If you do, you'll end up like me and spend an hour trying to figure out why you're getting this error.

    0 讨论(0)
  • 2020-11-22 06:33

    Even if I'm too late but this might help someone who will get stuck too in the coming days. I faced the same issue but my problem was running many commands with npm start. I was running create tables and insert data before start the app and make 10 sec exceeds before running. So I removed those commands and problem solved.

    Thanks

    0 讨论(0)
提交回复
热议问题