28

As per the official website, the correct way to save electron files is:

npm install electron --save-dev

Electron is actually required for running the app (quite literally: require()) and this goes against the top voted answer here. So why do we make this exception, if this is even one?

2 Answers 2

24

The fact that you require a package is irrelevant to whether it should be considered a dependency or a devDependency (in the npm sense). E.g. many projects use webpack API (i.e. const webpack = require('webpack')) but list it as a devDependency.

The reason is also explained in the post you link to: when you publish your package, if the consumer project needs other packages to use yours, then these must be listed as dependencies.

If your package uses some modules only for build, test, or bundles them into a dist file (i.e. what will be used by the consumer project), then those modules should not be mentioned in dependencies. We still list them in devDependencies for development.

Now in the case of an electron app, there is little chance you will consume your app as a node module of a consumer project, therefore the above convention is not really relevant.

Furthermore, we fall in the case where the electron package is bundled as part of the built output. There is no need for your user to get electron from npm to use your built app. Therefore it matches well the definition of a devDependency.

That being said, IIRC some electron packagers bundle your dependencies into the built app, so you still need some rigour in filling this list.

4
  • Can we say that if your project will not be used by other projects as a module, there is no difference between dependencies and devDependencies.? Most of the time we develop some server, website etc. which will not be used as a module, so most of the time devDependencies and dependencies are same.
    – fthdgn
    Jun 17, 2019 at 1:56
  • when i try to use electron-builder to create an executable, why do i get the following error: "electron" is only allowed in "devDependencies" stackoverflow electron builder
    – oldboy
    Feb 10, 2020 at 0:30
  • Hi @oldboy, i would guess you might be in the case described in the last paragraph of above answer, i.e. your packager tries to include your dependencies into your bundle, and fails to handle electron when it is in that list (it very probably knows how to handle it when omitted). Feel free to open a new question with details of your specific issue.
    – ghybs
    Feb 10, 2020 at 17:37
  • i figured it out. i simple moved it to dev dependencies, but i was under the impression that electron is an actual dependency, which is why it still doesnt make sense to me? anyways, now im running into another issue, and i have posted a question about it here .can you take a look if u dont mind?
    – oldboy
    Feb 10, 2020 at 21:27
3

Cause those binary won't being used when you actually packaging into installer. Most of installer / packager for electron will build package with electron binaries, instead of using dependencies.

1
  • 3
    Does that mean the lines with require('electron') get transpiled into something else? Or that the packager embeds electron?
    – Jacob
    Jun 11, 2018 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.