简体   繁体   中英

Include node_modules while packaging Electron desktop app?

I am trying out a simple Hello World Electron app in Linux. I want to package it and install it on Windows as well to test if it runs fine there.

When I did 'npm install', it created a node_modules folder of ~112 MB. I don't have anything yet that even makes use of these modules, and the sample starts fine if I delete this folder too. The package.json file has 'electron-prebuilt' as the only item in devDependencies so I assume the node_modules folder was for that.

So my doubt is should this folder be included in the final package if I want it to run on Windows?

I checked the documentation here but it just has the 3 files and no dependencies so I was not sure of what to do in cases where you explicitly have some.

Short answer:

You do need node_modules folder in your final distribution if you install additional libraries via npm and then reference them from your code.

Long answer:

I'm not sure, but you are probably confused because you installed electron-prebuilt , which is also an npm package, but this one is special, since it's required for development and packaging, and doesn't serve as a simple library for your app. I'd recommend using a two-package structure for your application to separate development and application packages. Here's an example of a folder structure for my electron app (let's call it MyApp ):

  • MyApp: solution root folder, for a simple app this would map onto your repository root folder

    • gulpfile.js - build configuration if you use gulp
    • package.json - dev package metadata. Dev package is not your application, but rather a wrapper around it.
    • other files, like .hgignore, README.md, whatever else.
    • node_modules : all dev dependencies, this will contain packages, which you need for development, like gulp , electron-prebuilt , gulp plugins, etc. exluded from repo
    • dist : distribution folder, this is where my gulp tasks are to put packaged app distribution when it goes to production. excluded from repo

    • src : all app sources. Now this is your application. All contents of this folder get packed into a distribution.

      • bower.json - bower manifest, in case you're using it
      • package.json - app package metadata
      • node_modules: all npm app dependencies (like sqlite, angular, d3, etc.); excluded from repo
      • bower_modules : all bower app dependencies excluded from repo
      • app.js - your app entry point

So, when I need to install a package, which helps me in development (usually a gulp plugin) I navigate to /MyApp and execute npm install -D [package] there. When I need to install a library for my app to use, like a front-end framework, I navigate to /MyApp/src and execute npm install -S [package] there.

When I need to quickly run electron app in development I feed /MyApp/src folder to gulp-run-electron plugin. Alternatively, if you installed electron-prebuilt for you dev package, you can navigate to /MyApp and run /MyApp/node_modules/electron-prebuilt/dist/electron[.exe] ./src . You can also install electron-prebuilt globally with npm i -g electron-prebuilt and just run electron ./src .

When I need to package my app for the release or testing I use gulp-electron plugin for gulp to produce a distribution. There are other ways to do this, like using electron-packager or one of its wrappers for a build system of your choice, or doing the whole thing manually.

Edit: Since you're concerned about the size, yeah prebuilt electron itself will take quite a lot of space, but the installer can be shrunk effectively with some archiver, like 7z.

electron-packager is a light-weight abstraction that takes some of the tedium out of building Electron applications for every platform.

It will go through your applications and create a executable for each of the operating systems that you specify. It will take care of bundling your application. You don't have to worry about specifying that node_modules be in the final build.

Generally speaking, it's not recommended to even check node_modules into version control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM