Electron-packager: Include only specific node_modules

Created on 31 Jan 2017  ·  17Comments  ·  Source: electron/electron-packager

Please describe your issue:
Let's assume that I want to exclude these files from packaging:

  • npm-debug
  • gulpfile.js
  • .vscode

So setting --ignore option like this:

--ignore='npm-debug|gulpfile\.js|\.vscode'

It's working. But how to I make regex that exclude only specific node_modules with above regex? I tried:

--ignode='npm-debug|gulpfile\.js|\.vscode|^((?!node_modules/mqtt).)$'  // NOT WORKING

but it won't worked. I know my regex was wrong, but I don't know how should I do now. Literally, I'm stucked now. Is there a something another option to include specific files from --ignore option? Else, how do I resolve this?

needs info question

Most helpful comment

@rico345100 If you don't want a node_module to be included in production, just set it as a devDepednency and it will be pruned automatically

All 17 comments

@rico345100 If you don't want a node_module to be included in production, just set it as a devDepednency and it will be pruned automatically

I already did, but with --ignore option, it's not working. This is my package.json:

{
  ...
  "devDependencies": {
    "aliasify": "^2.1.0",
    "babel-plugin-syntax-async-functions": "^6.13.0",
    "babel-plugin-transform-class-properties": "^6.19.0",
    "babel-plugin-transform-regenerator": "^6.20.0",
    "babel-polyfill": "^6.20.0",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "babelify": "^7.3.0",
    "browserify": "^13.1.1",
    "browserify-css": "^0.9.2",
    "electron-prebuilt": "^1.4.13",
    "eslint": "^3.13.1",
    "eslint-plugin-react": "^6.9.0",
    "envify": "^4.0.0",
    "gulp": "^3.9.1",
    "gulp-cssnano": "^2.1.2",
    "gulp-htmlmin": "^3.0.0",
    "gulp-rename": "^1.2.2",
    "gulp-sass": "^3.0.0",
    "gulp-uglify": "^2.0.0",
    "gulp-util": "^3.0.7",
    "isomorphic-fetch": "^2.2.1",
    "merge-stream": "^1.0.1",
    "moment": "^2.17.1",
    "persistify": "^1.1.1",
    "react": "^15.4.1",
    "react-copy-to-clipboard": "^4.2.3",
    "react-dom": "^15.4.1",
    "react-motion-ui-pack": "^0.10.2",
    "react-qr": "0.0.2",
    "react-redux": "^5.0.1",
    "react-router": "^3.0.0",
    "react-router-redux": "^4.0.7",
    "react-router-transition": "0.0.6",
    "redux": "^3.6.0",
    "scssify": "^2.2.0",
    "stringify": "^5.1.0",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0",
    "watchify": "^3.8.0",
    "yargs": "^6.5.0"
  },
  "dependencies": {
    "mqtt": "^2.1.3",
    "node-notifier": "^4.6.1"
  }
}

Move mqtt to devDependencies if you want to ignore it?

@MarshallOfSound No, I don't want to ignore mqtt and node-notifier. Those are I want to put inside of packaged app. So that's why I'm asking here.

@rico345100 electron-builder allows you to use convenient glob — https://github.com/electron-userland/electron-builder/wiki/Options#Config-files Maybe it is a solution for you. Also, consider to use yarn instead of npm to use yarn clean (electron-builder excludes npm-debug/.vscode by default, but https://yarnpkg.com/en/docs/cli/clean is much better).

@rico345100 Can you explain your current issue more precisely then please. Your initial issue indicated you wanted to ignore mgtt if that is not the case what exactly are you trying to acheive?

To summarize so far:

  • To ignore modules from your packaged app add them as devDependencies instead of dependencies

My issue is same from first, I want to put the only specific node modules inside of packaged app, and they are 'mqtt' and 'node-notifier', with using '--ignore' option.

I understood that electron-packager not includes node_modules, .vscode, npm-debug by default, but in my case, I have more files to exclude from package that I didn't mentioned it.

For instance, files that contain user configuration data created by node.js, and it shouldn't be included in packaged app. So basically, my problem wasn't solved just not using --ignore option.

These are actually I want to ignore from the packaged app, most of related development environment.

  • my-app-darwin-x64: packaged app for osx
  • my-app-win32-x64: packaged app for windows(ignoring these 2 packaged apps are for preventing including them self, like packaging them together with single command line)
  • userdata: User generated data
  • npm-debug
  • .vscode
  • gulpfile.js
  • gulpTasks: Seperated gulp scripts.

My question is simple: how do I include only "specific" node_modules, with using --ignore option? I don't wanna ignore mqtt, from the first to now, I never said like that.

I think you just little confused from my --ignore option that I tried:
--ignode='npm-debug|gulpfile.js|.vscode|^((?!node_modules/mqtt).)$'

The regex ^((?!node_modules/mqtt).)$ is for exclude every node modules, without node_modules/mqtt, not mqtt itself. There is no somekind of negate operator inside of RegExp, so I googgled for that, and that is just what am I tried from the post that I found, so that's not meaning that ignore mqtt, actually it's totally opposite.

I told you already my regex was wrong, so please understand that I'm not good at regex. I just want to know that using --ignore option to "not ignore" mqtt and node-notifier modules.

@rico345100

Untested but this regexp should do what you want

--ignore="^\/(.vscode|gulpfile.js|userdata.json|npm-debug.log|my-app-darwin-x64)"

Basically add things that you want to ignore inside those brackets. The key bit to that regexp is ^\/ which forces the match to be at the start of the path 👍

It's strongly based on this super custom regexp setup I have

https://github.com/MarshallOfSound/Google-Play-Music-Desktop-Player-UNOFFICIAL-/blob/master/gulpfile.babel.js#L78

You might find regexps a bit easier to work with as well if you use the node API instead of the CLI (just my opinion)

You shouldn't need to ignore my-app-darwin-x64 manually. If you do, that's a bug.

@MarshallOfSound I checked your code, and I'm integrated with gulp now(I didn't knew that there was programatic API). But still, my problem is same, not resolved at all.

I tried this: http://regexr.com/3f6c9

Into my code:

function buildExec(platform, done) {
    if(!{ DARWIN: 1, WIN32: 1 }[platform]) {
        throw new Error(`Unsupported platform ${platform}.`);
    }

    let icon = { DARWIN: 'icon-osx.icns', WIN32: 'icon-win.ico' }[platform];

    packager({
        asar: false,
        arch: 'x64',
        dir: global.__base,
        icon: icon,
        ignore: [
            () => /^((?!node_modules\/mqtt).)*$/g
        ],
        platform: platform.toLowerCase(),
    }, (err, appPaths) => {
        if(err) {
            throw new Error(err);
        }

        console.log(appPaths);
    });
}

Still all node_modules in my packaged app. In my electron project, there a hundreds of node_modules for development(especially babel related), so I can't ignore the modules one by one, there are still hundreds of modules are inside of node_modules, even not listed on packaged.json!

photo

Look at this photo, you can see that how many dev-related modules are in my node_modules, I just want to ignore them all, not only mqtt and node-notifier, they are needed.

Could you please run your gulp command with the environment variable DEBUG=electron-packager and paste the output here?

  electron-packager Electron Packager 8.5.1 +0ms
  electron-packager Node v6.9.2 +0ms
  electron-packager Host Operating system: darwin (x64) +1ms
  electron-packager Packager Options: {"asar":false,"arch":"x64","dir":"/Users/rico345100/Desktop/work/aladdin-wallet-pc","icon":"icon-osx.icns","ignore":[null],"platform":"darwin"} +0ms
  electron-packager Target Platforms: darwin +0ms
  electron-packager Target Architectures: x64 +0ms
  electron-packager Inferring application name from name in /Users/rico345100/Desktop/work/aladdin-wallet-pc/package.json +9ms
  electron-packager Inferring app-version from version in /Users/rico345100/Desktop/work/aladdin-wallet-pc/package.json +0ms
  electron-packager Inferring target Electron version from electron-prebuilt in /Users/rico345100/Desktop/work/aladdin-wallet-pc/package.json +3ms
  electron-packager Application name: aladdin-wallet-pc +0ms
  electron-packager Target Electron version: 1.4.13 +0ms
  electron-packager Ignored path regular expressions: [ [Function],
  '/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.13"} +5ms
Packaging app for platform darwin x64 using electron v1.4.13
  electron-packager Creating /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64-template +229ms
  electron-packager Extracting /Users/rico345100/.electron/electron-v1.4.13-darwin-x64.zip to /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64-template +0ms
  electron-packager Initializing app in /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64 from /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64-template template +992ms
  electron-packager Ignored paths based on the out param: [ '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-darwin-ia32',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-darwin-x64',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-darwin-armv7l',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-linux-ia32',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-linux-x64',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-linux-armv7l',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-mas-ia32',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-mas-x64',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-mas-armv7l',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-win32-ia32',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-win32-x64',
  '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-win32-armv7l' ] +2ms
  electron-packager Running npm prune --production +6s
  electron-packager Copying icon "icon-osx.icns" to app's Resources as "electron.icns" +7s
  electron-packager Renaming Electron to aladdin-wallet-pc in /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64/Electron.app/Contents/MacOS +2ms
  electron-packager Renaming Electron Helper to aladdin-wallet-pc Helper in /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64/Electron.app/Contents/Frameworks/Electron Helper.app/Contents/MacOS +1ms
  electron-packager Renaming Electron Helper.app to aladdin-wallet-pc Helper.app in /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64/Electron.app/Contents/Frameworks +0ms
  electron-packager Renaming Electron Helper EH to aladdin-wallet-pc Helper EH in /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64/Electron.app/Contents/Frameworks/Electron Helper EH.app/Contents/MacOS +0ms
  electron-packager Renaming Electron Helper EH.app to aladdin-wallet-pc Helper EH.app in /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64/Electron.app/Contents/Frameworks +0ms
  electron-packager Renaming Electron Helper NP to aladdin-wallet-pc Helper NP in /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64/Electron.app/Contents/Frameworks/Electron Helper NP.app/Contents/MacOS +1ms
  electron-packager Renaming Electron Helper NP.app to aladdin-wallet-pc Helper NP.app in /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64/Electron.app/Contents/Frameworks +0ms
  electron-packager Moving /var/folders/ch/ynbfklxj0ys670qqb93g79840000gn/T/electron-packager/darwin-x64/aladdin-wallet-pc-darwin-x64 to /Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-darwin-x64 +0ms
[ '/Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-darwin-x64' ]

Just so I'm clear, somewhere in /Users/rico345100/Desktop/work/aladdin-wallet-pc/aladdin-wallet-pc-darwin-x64 there's a node_modules directory with a bunch of babel modules in it? If that's the case, what is the absolute path of that directory?

Well, I don't know what was the problem, I rerun the packaging task, and it's now no dev related modules inside of it.

But still I have lots of not dependency modules in the packaged app:
still

Those modules doesn't take much spaces, but total files almost 1000. Are they needed to run Electron? Why are they inside of package?

@rico345100 Those modules will be dependencies of or sub-dependencies of your dependencies (mqtt and node-notifier)

Well then, I think it's resolved, but I still don't know what was the problem. Anyway thank you for helping me, I'm closing this issue now. Thanks again!

Was this page helpful?
0 / 5 - 0 ratings