packager: `electron-packager` is not bundling the Node modules listed in dependencies

I’ve been struggling with this for a week now so I thought I’d reach out to the amazing SO community for help. I’ve got an Electron project that needs to produce an OS X application (and Windows eventually). Initially we were using webpack to bundle everything (electron and front-end code) but, with the addition of some features that utilize ffi, this was no longer an option. After switching to using gulp to transpile and uglify the electron code, the app was working in development.

The problem I’m having is, when using electron-packager, none of my node_modules are being bundled during the build. According to the docs, electron-packager should bundle anything listed in the dependencies section of package.json. I built a simple proof-of-concept app to simplify things and modules still aren’t being bundled for the electron code. This new, simplified project is just trying to include bunyan (this also fails for ffi) but, after building, the app complains that the bunyan module cannot be found. Can someone help? Am I missing something small but important? Here’s some detail to the project:

Environment Details

  • OSX Version: 10.11.6 (15G1108)
  • Node Version: 5.2.0 (using nvm)
  • npm version: 3.3.12

package.json

    {
        "name": "build-with-native-modules",
        "version": "1.0.0",
        "description": "A test for Electron packages with native Node modules.",
        "main": "el/main.js",
        "scripts": {
          "test": "echo \"Error: no test specified\" && exit 1",
          "clean": "./node_modules/.bin/rimraf build",
          "start": "npm run clean && gulp && ./node_modules/.bin/electron ./build/dist/el/main.js",
          "electron:rebuild": "./node_modules/.bin/electron-rebuild",
          "build:osx": "gulp build:osx",
          "cinstall": "rm -Rf ./node_modules && npm cache clean && npm i && npm run electron:rebuild"
        },
        "postinstall": "install-app-deps",
        "author": "Zachary Abresch <zachary.abresch@gmail.com>",
        "license": "MIT",
        "dependencies": {
          "bunyan": "^1.8.4",
          "jquery": "^3.1.1"
        },
        "devDependencies": {
          "babel-cli": "^6.18.0",
          "babel-core": "^6.18.2",
          "babel-eslint": "^7.1.0",
          "babel-loader": "^6.2.7",
          "babel-preset-es2015": "^6.18.0",
          "babel-preset-stage-0": "^6.16.0",
          "babel-register": "^6.18.0",
          "electron": "1.4.6",
          "electron-packager": "^8.2.0",
          "electron-rebuild": "^1.3.0",
          "eslint": "^3.9.1",
          "eslint-config-airbnb": "^13.0.0",
          "eslint-plugin-import": "^2.2.0",
          "eslint-plugin-jsx-a11y": "^2.2.3",
          "eslint-plugin-react": "^6.6.0",
          "gulp": "^3.9.1",
          "gulp-babel": "^6.1.2",
          "gulp-util": "^3.0.7",
          "rimraf": "^2.5.4",
          "webpack": "^1.13.3",
          "webpack-dev-server": "^1.16.2"
        }
      }

gulpfile.babel.js

      import gulp from 'gulp';
      import babel from 'gulp-babel';
      import gutil from 'gulp-util';
      import webpack from 'webpack';
      import packager from 'electron-packager';
    
      import webpackConfig from './webpack.config.babel';
      import pack from './package.json';
    
      gulp.task('electron:babel', () => {
        gulp.src('src/el/**/*.js', { base: 'src' })
        .pipe(babel())
        .pipe(gulp.dest('build/dist'));
      });
    
      gulp.task('move:html', () => {
        gulp.src('src/ui/**/*.html', { base: 'src' })
        .pipe(gulp.dest('build/dist'));
      });
    
      gulp.task('move:package', () => {
        gulp.src('package.json', { base: './' })
          .pipe(gulp.dest('build/dist'));
      });
    
      gulp.task('ui:webpack', (done) => {
        const myConfig = Object.create(webpackConfig);
    
        webpack(myConfig, (err, stats) => {
          if (err) throw new gutil.PluginError('ui:webpack', err);
          gutil.log('[ui:webpack]', stats.toString({ colors: true }));
          done();
        });
      });
    
      const osxBuildOptions = {
        arch: 'x64',
        'app-copyright': '8x8, Inc.',
        'app-version': '1.0.0',
        'build-version': '1.0.0',
        dir: './build/dist',
        name: pack.name,
        out: './build/bundle',
        overwrite: true,
        version: pack.devDependencies.electron,
      };
    
      gulp.task('build:osx',
        ['move:package', 'electron:babel', 'ui:webpack', 'move:html'],
      (done) => {
        packager(osxBuildOptions, (err, appPath) => {
          if (err) throw new gutil.PluginError('electron-packager', err);
          gutil.log(`[build:osx] App built in: ${appPath}`);
          done();
        });
      });
    
      gulp.task('default', ['electron:babel', 'ui:webpack', 'move:html']);

webpack.config.babel.js

    import path from 'path';
    
      module.exports = {
        target: 'electron',
        entry: './src/ui/renderer.js',
        output: {
          path: path.resolve(__dirname, 'build/dist/ui'),
          filename: 'ui.bundle.js',
        },
        module: {
          loaders: [
            {
              loader: 'babel-loader',
              include: [
                path.resolve(__dirname, 'src/ui'),
              ],
              test: /\.js/,
            },
          ],
        },
      };

I’m considering trying out electron-builder but I really feel like this should be working. If anyone has any info or insight I’d greatly appreciate it. If you need any additional information, please let me know in the comments. Thanks!

Console output when you run electron-packager with the environment variable DEBUG=electron-packager. Please include the stack trace if one exists.

$> DEBUG=electron-packager npm run build:osx

> build-with-native-modules@1.0.0 build:osx /Users/zabresch/Documents/8x8/scratching/build-with-native-modules
> gulp build:osx

[10:33:26] Requiring external module babel-register
[10:33:27] Using gulpfile ~/Documents/8x8/scratching/build-with-native-modules/gulpfile.babel.js
[10:33:27] Starting 'move:package'...
[10:33:27] Finished 'move:package' after 8.18 ms
[10:33:27] Starting 'electron:babel'...
[10:33:27] Finished 'electron:babel' after 3.8 ms
[10:33:27] Starting 'ui:webpack'...
[10:33:27] Starting 'move:html'...
[10:33:27] Finished 'move:html' after 924 μs
[10:33:27] [ui:webpack] Hash: b17668fbc87d8c088dcc
Version: webpack 1.13.3
Time: 398ms
       Asset    Size  Chunks             Chunk Names
ui.bundle.js  278 kB       0  [emitted]  main
chunk    {0} ui.bundle.js (main) 268 kB [rendered]
    [0] ./src/ui/renderer.js 449 bytes {0} [built]
    [1] ./~/jquery/dist/jquery.js 267 kB {0} [built]
    [2] external "electron" 42 bytes {0} [not cacheable]
[10:33:27] Finished 'ui:webpack' after 418 ms
[10:33:27] Starting 'build:osx'...
  electron-packager Electron Packager 8.2.0 +0ms
  electron-packager Node v5.2.0 +1ms
  electron-packager Host Operating system: darwin (x64) +0ms
  electron-packager Packager Options: {"arch":"x64","app-copyright":"8x8, Inc.","app-version":"1.0.0","build-version":"1.0.0","dir":"./build/dist","name":"build-with-native-modules","out":"./build/bundle","overwrite":true,"version":"1.4.6"} +0ms
  electron-packager Target Platforms: darwin +0ms
  electron-packager Target Architectures: x64 +0ms
  electron-packager Application name: build-with-native-modules +1ms
  electron-packager Target Electron version: 1.4.6 +0ms
  electron-packager Ignored path regular expressions: [ '/node_modules/electron($|/)',
  '/node_modules/electron-prebuilt($|/)',
  '/node_modules/electron-packager($|/)',
  '/\\.git($|/)',
  '/node_modules/\\.bin($|/)',
  '\\.o(bj)?$' ] +0ms
  electron-packager Downloading Electron with options {"platform":"darwin","arch":"x64","version":"1.4.6"} +6ms
Packaging app for platform darwin x64 using electron v1.4.6
  electron-packager Creating /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64-template +240ms
  electron-packager Extracting /Users/zabresch/.electron/electron-v1.4.6-darwin-x64.zip to /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64-template +0ms
  electron-packager Initializing app in /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64 from /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64-template template +1s
  electron-packager Ignored paths based on the out param: [ '/Users/zabresch/Documents/8x8/scratching/build-with-native-modules/build/bundle' ] +2ms
  electron-packager Running npm prune --production +9ms
  electron-packager Renaming Electron to build-with-native-modules in /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64/Electron.app/Contents/MacOS +647ms
  electron-packager Renaming Electron Helper to build-with-native-modules Helper in /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64/Electron.app/Contents/Frameworks/Electron Helper.app/Contents/MacOS +0ms
  electron-packager Renaming Electron Helper.app to build-with-native-modules Helper.app in /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64/Electron.app/Contents/Frameworks +1ms
  electron-packager Renaming Electron Helper EH to build-with-native-modules Helper EH in /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64/Electron.app/Contents/Frameworks/Electron Helper EH.app/Contents/MacOS +0ms
  electron-packager Renaming Electron Helper EH.app to build-with-native-modules Helper EH.app in /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64/Electron.app/Contents/Frameworks +1ms
  electron-packager Renaming Electron Helper NP to build-with-native-modules Helper NP in /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64/Electron.app/Contents/Frameworks/Electron Helper NP.app/Contents/MacOS +0ms
  electron-packager Renaming Electron Helper NP.app to build-with-native-modules Helper NP.app in /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64/Electron.app/Contents/Frameworks +0ms
  electron-packager Moving /var/folders/zx/m4gltph949j450_vsv_pt4mjzphjyg/T/electron-packager/darwin-x64/build-with-native-modules-darwin-x64 to build/bundle/build-with-native-modules-darwin-x64 +1ms
[10:33:29] [build:osx] App built in: build/bundle/build-with-native-modules-darwin-x64
[10:33:29] Finished 'build:osx' after 2.02 s

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 32 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I am seeing the same with electron-packager. I am not using yarn or gulp or any other build tool. When I run electron-packager . --overwrite --platform=darwin --arch=x64 --icon=assets/icons/mac/icon.icns --overwrite=true --out=release-builds to build and run the build, I get the same error about things not in the package. However when I run electron . it runs fine in development. I’ve looked in the .app package contents from the generated stuff and I don’t see everything in node_modules.

Okay … after a bit of digging I was able to figure out how to run npm i in build/dist. Here’s the gulp task that got it done:

// Installs dependencies into build folder
gulp.task('run:npm-install', shell.task([
  'npm --prefix ./build/dist install ./build/dist --production',
  './node_modules/.bin/electron-rebuild -m ./build/dist/node_modules',
]));

The --production option is key since that only installs dependencies in package.json. Thank you so much for your help on this!

@malept I’m also running into this. It seems like electron-packager doesn’t play well with npm packages that have been hoisted to a node_modules parent folder. Is that by design?

We’re using yarn workspaces and npm packages get hoisted automatically to the root of the mono repo.

Oh, I see. You are copying the app to build/dist first. Does node_modules (and those packages) exist in that directory?

So the issue that I had was due to using src in the ignore option in the building config object. I had the following ignore regex (which was the default for https://github.com/SimulatedGREG/electron-vue)

building: {
    ignore: /\b(src|index\.ejs|icons)\b/,`
}

However this also ignores src folders in node_modules that have to be copied. One npm module that I had issues with was the debug module https://github.com/visionmedia/debug#readme. As you can see there is a src folder in the module which is needed for the module to function. However, because it’s ignored, it will throw an error on launch.

Solution: I have renamed my src folder to sourcecode and changed the regex string to /\b(sourcecode|index\.ejs|icons)\b/ (and all references to it) now it runs fine in production.

So the issue that I had was due to using src in the ignore option in the building config object. I had the following ignore regex (which was the default for https://github.com/SimulatedGREG/electron-vue)

building: {
    ignore: /\b(src|index\.ejs|icons)\b/,`
}

However this also ignores src folders in node_modules that have to be copied. One npm module that I had issues with was the debug module https://github.com/visionmedia/debug#readme. As you can see there is a src folder in the module which is needed for the module to function. However, because it’s ignored, it will throw an error on launch.

Solution: I have renamed my src folder to sourcecode and changed the regex string to /\b(sourcecode|index\.ejs|icons)\b/ (and all references to it) now it runs fine in production.

Thanks for your answer. It’s very helpful for me!👍

Hi , i’m having the same issue . tried all of the above and still it doesn’t work. someone have some other idea?

@cballantyne

npm version - 5.6.0

You need to delete your package-lock.json file as well to get newer modules 👍

I was experiencing the same issue, but I wasn’t using Gulp loaders, so that couldn’t be it.

For me, the problem was that main.js was located in ./src/app, which does not contain the node_modules folder.

main.js has to be in the root folder along with package.json and node_modules, that solves the problem.

I was starting from this angular-electron starter which had the file placed there.

I was under the impression that electron-packager would bundle anything listed in dependencies in package.json. Do I need to manually copy node_modules somewhere? If so, is there a clean way to do that with just the dependencies?

Yeah, did that and, no, the node_modules folder is not there. I tried it with asar: false too just to see if the app folder would contain it. No luck.