Search code examples
electronelectron-packager

electron-packager and gloabl electron module


I have installed electron and electron-packager, all of them in global mode. When I build my app electron-packager searchs the local electron module. How forcing electron-packager to use the global electron module that I have installed?


Solution

  • The short answer is that what you have described isn't the way you "should" use electron-packager. Normally, the intent is that you are building a local package (exe or such) under the project directory you are working on. For example, an electron/angular project building on a Windows platform might have the following kind of structure:

    C:.
    +---ClientSide
    ¦   +---index.html
    ¦   +---app
    ¦   ¦   +---app.component.ts
    ¦   ¦   +---app.module.ts
    ¦   ¦   +---main.ts
    ¦   ¦   +---AppContent/
    ¦   ¦   +---help/
    ¦   +---Styles
    ¦   +---test
    ¦       +---AppContent/
    +---dist/
    +---edist
    |   \---Application-win32-ia32 [*location of binary source for the install]
    +---Installer
        +---Application/
    gulpfile.js
    karma.conf.js
    main.js
    package.json
    README.md
    webpack.config.js
    

    In this kind of scenario, the package.json file typically contains reference to both packages, as in:

    .. .. ..
      "devDependencies": {
        "@angular/animations": "4.4.4",
        "@angular/common": "4.4.4",
        "@angular/compiler": "4.4.4",
    .. .. ..
    .. .. ..
        "electron": "1.7.9",
        "electron-packager": "9.1.0",
    .. .. ..
    

    Then within your local gulpfile.js you would typically include a call to run the packager that refers to the local version of electron. Something like:

    'use strict';
    ...   ...
    var packager = require('electron-packager');
    var electronPackage = require('electron/package.json');
    var pkg = require('./package.json');
    // pull the electron version from the package.json file
    var electronVersion = electronPackage.version;
    ...   ...
    
    var opts = {
        name: pkg.name,
        platform: 'win32',
        arch: 'ia32',                           // ia32, x64 or all
        dir: './',                       // source location of app
        out: './edist/',              // destination location for app os/native binaries
        ignore: config.electronignore,          // don't include these directories in the electron app build
        icon: config.icon,
        asar: {unpackDir: config.electroncompiled}, // compress project/modules into an asar blob but don't use asar to pack the native compiled modules
        overwrite: true,
        prune: true,
        electronVersion: electronVersion ,       // Tell the packager what version of electron to build with
        appCopyright: pkg.copyright,            // copyright info
        appVersion: pkg.version,         // The version of the application we are building
        win32metadata: {                        // Windows Only config data
            CompanyName: pkg.authors,
            ProductName: pkg.name,
            FileDescription: pkg.description,
            OriginalFilename: pkg.name + '.exe'
        }
    };
    
    
    // Build the electron app
    gulp.task('build:electron', function (cb) {
    
        console.log('Launching task to package binaries for ' + opts.name + ' v' + opts['appVersion']);
    
        packager(opts, function (err, appPath) {
            console.log(' <- packagerDone() ' + err + ' ' + appPath);
            console.log(' all done!');
            cb();
        });
    });
    

    If you don't want to build the same version of electron that as is present locally, you can change that parameter to whatever version of electron you'd like packager to use. As in, replacing this line of code:

    // pull the electron version from the package.json file
    var electronVersion = electronPackage.version;
    

    With something like this:

    // Use a specific electron version
    var electronVersion = '1.7.8';
    

    If you are going to run electron-packager from the command line, you have all the same options available as I've shown here in the API options. You can see the full list of options in their online github user docs . In your case, if you are using command line then use the "--electron-version" switch to set the electron version you wish.