How to Create a Windows Executable with Electron Forge that Adds a Desktop Shortcut?

WHAT TO KNOW - Sep 21 - - Dev Community
<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
  <title>
   Creating Windows Executables with Electron Forge and Desktop Shortcuts
  </title>
  <style>
   body {
            font-family: sans-serif;
        }
        h1, h2, h3 {
            margin-top: 2rem;
        }
        pre {
            background-color: #f0f0f0;
            padding: 1rem;
            border-radius: 5px;
        }
        code {
            font-family: monospace;
        }
  </style>
 </head>
 <body>
  <h1>
   Creating Windows Executables with Electron Forge and Desktop Shortcuts
  </h1>
  <h2>
   Introduction
  </h2>
  <p>
   Electron Forge is a powerful tool that simplifies the process of creating cross-platform desktop applications using web technologies like HTML, CSS, and JavaScript. In this comprehensive guide, we'll explore how to leverage Electron Forge to build Windows executables for your applications and, importantly, how to seamlessly integrate desktop shortcuts for a user-friendly experience.
  </p>
  <p>
   This guide is relevant for developers who want to create robust desktop applications that look and feel native on Windows while offering the flexibility of web development. Creating desktop shortcuts enhances user accessibility and streamlines the launch process.
  </p>
  <h2>
   Key Concepts, Techniques, and Tools
  </h2>
  <h3>
   Electron
  </h3>
  <p>
   Electron is the core technology underpinning our endeavor. It's an open-source framework that allows you to build desktop applications using web technologies. Essentially, Electron bundles Chromium (the open-source browser engine behind Google Chrome) and Node.js, enabling you to build user interfaces with familiar tools and access the power of Node.js for backend operations.
  </p>
  <h3>
   Electron Forge
  </h3>
  <p>
   Electron Forge acts as a scaffolding and build tool for Electron projects. It streamlines the development process by providing:
  </p>
  <ul>
   <li>
    <strong>
     Project setup and configuration:
    </strong>
    Easily creates the initial structure and configuration files for your Electron application.
   </li>
   <li>
    <strong>
     Package management:
    </strong>
    Simplifies dependency management with tools like npm and Yarn.
   </li>
   <li>
    <strong>
     Build automation:
    </strong>
    Automates the process of packaging your application into executable files for various platforms.
   </li>
   <li>
    <strong>
     Plugin system:
    </strong>
    Offers a flexible way to extend Forge's functionality with plugins for tasks like code signing, auto-updating, and more.
   </li>
  </ul>
  <h3>
   Windows Desktop Shortcuts
  </h3>
  <p>
   Desktop shortcuts provide a quick and convenient way for users to launch your application. They essentially act as pointers to the actual executable file, providing an easy-to-access entry point.
  </p>
  <h2>
   Practical Use Cases and Benefits
  </h2>
  <p>
   Building desktop applications with Electron Forge offers numerous advantages:
  </p>
  <ul>
   <li>
    <strong>
     Cross-platform Compatibility:
    </strong>
    Develop once and deploy your application across multiple operating systems (Windows, macOS, Linux) without substantial code changes.
   </li>
   <li>
    <strong>
     Rich User Interfaces:
    </strong>
    Utilize the power of web technologies (HTML, CSS, JavaScript) to create modern, interactive, and engaging UIs.
   </li>
   <li>
    <strong>
     Native Features:
    </strong>
    Access native operating system features like file system access, notifications, and more through Node.js and Electron's APIs.
   </li>
   <li>
    <strong>
     Faster Development:
    </strong>
    Benefit from the familiarity of web technologies and the streamlined development process provided by Electron Forge.
   </li>
   <li>
    <strong>
     Improved User Experience:
    </strong>
    Desktop shortcuts enhance usability by offering a convenient launch point for your application.
   </li>
  </ul>
  <h2>
   Step-by-Step Guide: Creating a Windows Executable with Desktop Shortcuts
  </h2>
  <p>
   Let's walk through the process of creating a basic Windows executable using Electron Forge and including a desktop shortcut. You'll need to have Node.js and npm installed on your system.
  </p>
  <ol>
   <li>
    <h3>
     Initializing Your Electron Project
    </h3>
    <p>
     Start by creating a new Electron Forge project using the following command:
    </p>
    <code>
     npm init electron-forge@latest my-electron-app
    </code>
    <p>
     Replace "my-electron-app" with your desired project name. This command will initialize a new project directory with the necessary files and configurations.
    </p>
   </li>
   <li>
    <h3>
     Setting Up the Application's Structure
    </h3>
    <p>
     Navigate into your project directory:
    </p>
    <code>
     cd my-electron-app
    </code>
    <p>
     The project structure should look something like this:
    </p>
    <code>
     my-electron-app/
        ├── src/
        │   ├── main/
        │   │   ├── index.js
        │   │   └── preload.js
        │   ├── renderer/
        │   │   └── index.html
        │   └── index.js
        ├── package.json
        ├── forge.config.js
        └── webpack.config.js
    </code>
    <p>
     The "src" directory holds your application's code. The "renderer" subdirectory contains your web frontend (HTML, CSS, JavaScript), while "main" handles the backend logic. The "package.json" file specifies project dependencies and configuration, while "forge.config.js" configures Electron Forge.
    </p>
   </li>
   <li>
    <h3>
     Developing the Frontend
    </h3>
    <p>
     In the "renderer" subdirectory, you'll build your UI with HTML, CSS, and JavaScript. For this example, we'll create a simple "Hello World" application.
    </p>
    <p>
     Replace the contents of "renderer/index.html" with the following:
    </p>
    <code>
     <!DOCTYPE html>
     <html>
      <head>
       <title>
        My Electron App
       </title>
      </head>
      <body>
       <h1>
        Hello World!
       </h1>
      </body>
     </html>
    </code>
   </li>
   <li>
    <h3>
     Configuring Electron Forge
    </h3>
    <p>
     Open the "forge.config.js" file and add the following configurations:
    </p>
    <code>
     const forgeConfig = {
        packagerConfig: {
            executableName: 'my-electron-app', // Sets the executable name
            asar: true, // Enable asar packaging for security and efficiency
            icon: 'src/assets/icon.ico', // Path to your application icon (create an icon.ico file in the "src/assets" directory)
            ignore: [
                // Ignore unnecessary files and folders during packaging
                'build', 
                'dist', 
                '.git', 
                '.gitignore'
            ],
            extraResources: [
                {
                    from: 'src/assets/',
                    to: 'assets/'
                }
            ]
        },
        makers: [
            {
                name: '@electron-forge/maker-squirrel', // Use the Squirrel installer for Windows
                config: {
                    name: 'my-electron-app' // Sets the installer name
                }
            },
            {
                name: '@electron-forge/maker-zip', // Create a zip archive for distribution
                platforms: ['darwin'] // Include macOS as a platform
            }
        ]
    };

    module.exports = forgeConfig;
    </code>
    <p>
     This configuration specifies the executable name, icon, and uses the Squirrel installer for Windows. It also creates a zip archive for macOS. Make sure to create an "icon.ico" file in the "src/assets" directory and replace it with your application icon.
    </p>
   </li>
   <li>
    <h3>
     Adding a Desktop Shortcut (Windows)
    </h3>
    <p>
     Electron Forge does not directly create desktop shortcuts. We'll use a script to automate the process. Create a new file named "create-shortcut.js" in the root directory of your project and add the following code:
    </p>
    <code>
     const fs = require('fs');
    const path = require('path');
    const shell = require('shelljs');

    // Function to create a desktop shortcut
    function createShortcut(targetPath, shortcutPath) {
        // Ensure the shortcut path exists
        const shortcutDir = path.dirname(shortcutPath);
        if (!fs.existsSync(shortcutDir)) {
            fs.mkdirSync(shortcutDir, { recursive: true });
        }

        // Create the shortcut using Windows command
        const cmd = `cmd /c "mklink /J "${shortcutPath}" "${targetPath}"`;
        shell.exec(cmd);
    }

    // Main function
    function main() {
        // Get the current directory
        const currentDir = process.cwd();

        // Define the target executable path
        const targetPath = path.join(currentDir, 'dist', 'win32', 'my-electron-app', 'my-electron-app.exe');

        // Define the shortcut path (replace with your desired shortcut location)
        const shortcutPath = path.join(currentDir, 'dist', 'win32', 'my-electron-app', 'my-electron-app.lnk');

        // Create the shortcut
        createShortcut(targetPath, shortcutPath);
    }

    // Run the main function
    main();
    </code>
    <p>
     This script will create a shortcut named "my-electron-app.lnk" in the "dist/win32/my-electron-app" directory, pointing to the executable. Adjust the shortcut path and directory structure if needed.
    </p>
   </li>
   <li>
    <h3>
     Building the Application
    </h3>
    <p>
     Now you can build your application using the following command:
    </p>
    <code>
     npm run make
    </code>
    <p>
     This will create a folder named "dist" in your project directory, containing the built executables and installers for Windows and macOS. You'll find the executable file inside "dist/win32/my-electron-app" along with the shortcut.
    </p>
   </li>
   <li>
    <h3>
     Running and Testing
    </h3>
    <p>
     To test your application, simply run the executable file located in "dist/win32/my-electron-app/my-electron-app.exe". You should see your "Hello World" window appear.
    </p>
   </li>
   <li>
    <h3>
     Distributing Your Application
    </h3>
    <p>
     You can now distribute your application by providing users with the installer created in "dist/win32/my-electron-app/my-electron-app-setup.exe" for Windows.
    </p>
   </li>
  </ol>
  <h2>
   Challenges and Limitations
  </h2>
  <p>
   While Electron Forge simplifies desktop application development, you might encounter some challenges:
  </p>
  <ul>
   <li>
    <strong>
     Performance:
    </strong>
    Electron applications can consume more resources (CPU and memory) than native applications due to their web-based nature. Careful optimization is essential for performance.
   </li>
   <li>
    <strong>
     Security:
    </strong>
    Because Electron uses Chromium, it inherits some security vulnerabilities. Proper security practices and code reviews are crucial.
   </li>
   <li>
    <strong>
     Platform-Specific Issues:
    </strong>
    You might need to handle platform-specific quirks and features, potentially requiring separate code branches or additional libraries.
   </li>
   <li>
    <strong>
     Shortcut Limitations:
    </strong>
    While the script provides a basic shortcut, advanced shortcut features (like custom icons, descriptions, and keyboard shortcuts) require deeper Windows API integration.
   </li>
  </ul>
  <h2>
   Comparison with Alternatives
  </h2>
  <p>
   Here are some alternatives to Electron Forge for creating desktop applications:
  </p>
  <ul>
   <li>
    <strong>
     NW.js:
    </strong>
    Similar to Electron, NW.js uses web technologies but focuses on simpler integration and smaller package sizes.
   </li>
   <li>
    <strong>
     Tauri:
    </strong>
    Tauri aims for smaller bundle sizes and improved security by utilizing Rust and WebAssembly.
   </li>
   <li>
    <strong>
     React Native:
    </strong>
    While primarily for mobile apps, React Native can also be used for cross-platform desktop apps using its "Desktop" platform.
   </li>
   <li>
    <strong>
     Native Development:
    </strong>
    For maximum performance and native features, native development languages like C++, C#, or Java might be preferable.
   </li>
  </ul>
  <p>
   Choosing the best approach depends on your specific needs, performance requirements, and development expertise.
  </p>
  <h2>
   Conclusion
  </h2>
  <p>
   Electron Forge empowers developers to create engaging desktop applications with web technologies. The combination of Electron, Node.js, and the efficient tooling of Electron Forge allows for rapid development cycles. By using the script provided, you can easily add desktop shortcuts to enhance user accessibility on Windows. While challenges exist, the benefits of cross-platform compatibility, rich user interfaces, and native features make Electron Forge a compelling option for building desktop applications.
  </p>
  <h2>
   Further Learning
  </h2>
  <ul>
   <li>
    Electron Forge documentation:
    <a href="https://www.electronforge.io/">
     https://www.electronforge.io/
    </a>
   </li>
   <li>
    Electron documentation:
    <a href="https://www.electronjs.org/">
     https://www.electronjs.org/
    </a>
   </li>
   <li>
    Windows API for advanced shortcut customization:
    <a href="https://docs.microsoft.com/en-us/windows/win32/shell/shortcut-objects">
     https://docs.microsoft.com/en-us/windows/win32/shell/shortcut-objects
    </a>
   </li>
  </ul>
  <h2>
   Call to Action
  </h2>
  <p>
   Try building your own desktop application using Electron Forge! You can experiment with different UI designs, features, and integrations. Explore the world of desktop development with the power and flexibility of web technologies.
  </p>
 </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Explanation of the Code and Steps:

  1. Initialization: We start by creating a new Electron Forge project using the npm init electron-forge@latest command. This creates the basic project structure.

  2. Project Structure: The project directory contains "src" for source code, "renderer" for frontend code, and "main" for backend logic. We'll focus on modifying the files in these directories.

  3. Frontend Development: We create a simple "Hello World" UI in "renderer/index.html".

  4. Electron Forge Configuration: We configure Electron Forge in "forge.config.js" to:

    • Set the executable name.
    • Include the application icon.
    • Specify the installer using the Squirrel installer for Windows.
    • Create a zip archive for macOS.
  5. Desktop Shortcut Script: We create a script "create-shortcut.js" to automatically create a desktop shortcut pointing to the executable file.

  6. Building the Application: We use npm run make to build the application, creating the "dist" folder with the executables, installers, and shortcuts.

  7. Running and Testing: We run the executable to test the application.

Note: The provided HTML code includes basic styling for readability and code blocks. You can customize it further to enhance the visual presentation of your article.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player