4

I am developing a NodeJS application. It will be deployed with Electron. This is good because all I will need to do is to package my application in an app folder inside the Electron package, then rebrand it as described on the official Electron documentation, and I will be ready to install it on any computer.

In particular, deploying with Electron means that I don't need to have NodeJS installed on my user's computer, because the Electron package is itself a NodeJS interpreter so it is going to run out of the box completely on its own.

Now, when I installed NodeJS on my system I also got npm installed along with it. Which means that whenever I need to install a package, I can just use it from command line.

Now, the program I need to deploy will keep a repository synced and updated by pulling it from github when needed. I found a wonderful NodeJS binding for Git, nodegit, that works on its own without any need of installing anything on the target computer, which is good because I am not sure my users will have git installed. Now, every time I pull my repository I will also need to do some npm install.

Which brings up my question: I know that there is a library npm that I can use to use npm programmatically from any NodeJS program. However, is that npm library a standalone thing, or it depends on the installation of npm on the system? If so, how can I work around this problem? I need to be able to do an npm install from an Electron app deployed somewhere on an user environment that I can't assume to have anything else installed, npm in particular.

Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
  • Just keep in mind that your users won't be able to install any NPM package that directly or indirectly depends on native Node addons, because native addons have to be rebuilt to target the Electron runtime. – Vadim Macagon Feb 11 '16 at 03:57
  • Maybe I'm wrong but I would expect an electron app to come with all dependencies embedded since it's supposed to be a standalone app. – Shanoor Feb 11 '16 at 04:37

2 Answers2

1

npm install --save npm

Not sure why I thought that wouldn't work, but I tested it and it seems too :)

Max
  • 4,529
  • 1
  • 25
  • 29
  • Could you give me an example of how you did that? What I did was opening an electron.app binary, create an app folder in Contents/Resources, then `npm install npm` inside it. I got `npm` in `node_modules`, then I wrote down a sketch that would `chdir` to a known location, and then `npm.load(function(error){npm.commands.install(['sqlite3'], function(error, data){console.log(error);})})` (`sqlite3` being just an example) and I got a whole bunch of errors originating from `life_cycle.js`. – Matteo Monti Feb 15 '16 at 15:39
  • Like, if you had an actual `electron.app` (or equivalent) with npm packaged in it and actually working, it would be so great. Do you think you could upload it somewhere so that I can see how you did it? – Matteo Monti Feb 15 '16 at 15:40
0

If you install npm locally like you would any other process you should be able to use it just the same.

var npm = require('npm');

Then from there just use git and then install your packages.

Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48
  • I am sorry, but I am not sure to get your point. What do you mean by "install npm locally"? I would need to deploy it along with my application somehow.. – Matteo Monti Feb 11 '16 at 10:12
  • But I cannot significantly affect my user's environment. I mean if I get my application installed, I can't just install npm on their computer. I would like to keep everything in my package. – Matteo Monti Feb 11 '16 at 10:13
  • If Electron packages all of your other modules with it then you should be able to get it to do the same with npm then you'd have a copy of it inside of your app meaning you can use `npm install`. – Alexis Tyler Feb 11 '16 at 12:59