How to change electron-forge default port?
Asked Answered
S

5

9

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
  }
};
Shackle answered 2/3, 2020 at 8:43 Comment(0)
Z
15

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
          }
        ]
      ]
    }
  }
}
Zygoma answered 10/4, 2020 at 22:30 Comment(2)
2022: This did the trick, thank youLavaliere
does not work for meThorazine
N
2

Similar to what @ferm10n had for an answer, but I made the change in the root file forge.config.ts instead. A port property that hangs off the WebpackPlugin object.

const config: ForgeConfig = {
  packagerConfig: {
    asar: true,
  },
  rebuildConfig: {},
  makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})],
  plugins: [
    new AutoUnpackNativesPlugin({}),
    new WebpackPlugin({
      mainConfig,
      renderer: {
        config: rendererConfig,
        entryPoints: [
          {
            html: './src/index.html',
            js: './src/renderer.ts',
            name: 'main_window',
            preload: {
              js: './src/preload.ts',
            },
          },
        ],
      },
      port: 3050 // <--- CUSTOM PORT
    }),
  ],
};
Naphthol answered 14/7, 2023 at 10:17 Comment(1)
2023 this worksCaballero
C
1

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

Careen answered 2/2, 2022 at 20:5 Comment(0)
I
1

$ PORT=3050 npm run dev works fine

Inerney answered 12/6, 2023 at 10:14 Comment(0)
R
0

Follow instructions here to setup Webpack configuration for your project (if not already done): https://www.electronforge.io/config/plugins/webpack

Then, follow the instructions here to change the devServer.port option: https://webpack.js.org/configuration/dev-server/

Rutter answered 2/3, 2020 at 14:54 Comment(3)
where is devServer is located? my webpack.main.config.js file doesn't have any, and if I add devServer, it gets ignored by electron. Still running on 3000Shackle
It should work when you add devServer and its options. Can you post a copy of your webpack config?Rutter
OK I added my webpack configShackle

© 2022 - 2024 — McMap. All rights reserved.