How to change electron-forge default port?

Viewed 3570

I can't find reference about this anywhere in their website/docs. I need to change this to another port, currently it's stuck in 3000.

Here's how I create an electron project with electron-forge

yarn create electron-app my-new-app --template=typescript

and whenever there is another service in port 3000. It throws an error:

listen EADDRINUSE :::3000
Error: listen EADDRINUSE :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1360:14)

my webpack.main.config.js file:

const path = require('path');
module.exports = {
  entry: './src/index.ts',
  // Put your normal webpack config below here
  module: {
    rules: require('./webpack.rules')
  },
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json']
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 9000
  }
};
3 Answers

I just started out using electron-forge and ran into the same issue, and it seemed like the electron/webpack was ignoring my devServer.port value.

After finding this doc for the electron-forge config I realized the devServer.port is probably overwritten by electron-forge, and the values for the config were set in package.json. Here was mine:

{
  "name": "my-project",
  // ...
  "config": {
    "forge": {
      "packagerConfig": {},
      "makers": [
        // ...
      ],
      "plugins": [
        [
          "@electron-forge/plugin-webpack",
          {
            "mainConfig": "./webpack.main.config.js",
            "renderer": {
              "config": "./webpack.renderer.config.js",
              "entryPoints": [
                {
                  "html": "./src/index.html",
                  "js": "./src/renderer.js",
                  "name": "main_window"
                }
              ]
            },

            // the secret sauce:
            "port": 3001,
            "loggerPort": 9001
          }
        ]
      ]
    }
  }
}

As an update to this question, I was unable to replicate the top-rated answer and eventually changed the default port with a slightly different approach by implementing this answer from another post.

Electron-forge with webpack devServer

Related