1

I wrote an app with Electron js that encapsulate Ionic apps for editing them live. So i use a child process to cd {ionic app path} && ionic serve/npm install/npm update for serving and updating packages of the live Ionic app in my Electron container. No problems with this technique on my side. But when i package my app and use an installer to test it on a clean machine, npm cannot be executed because of nodejs that is not installed on it.

First I was thinking of including a nodejs installer into my main app installer but this does not seem to me that is the good way of doing it.

And after digging on stackoverflow I've found this thread: Install programmatically a NPM package providing its version that explain how to use npm directly in my code with require("npm"); and that's worked but i was not able to tell npm.install() in which folder i want to run this command what was possible with child process.

I has tried to read the lib but this not seams to be possible: https://github.com/npm/npm/blob/latest/lib/install.js

Do you have any idea what I can do to solve this problem ?

Tim
  • 41
  • 2
  • 10

1 Answers1

0

So I've found the answer after digging into this code https://github.com/npm/npm/blob/latest/lib/install.js Simply use npm like this :

npm.load({}, function (err) {
  npm.commands.install(HERE_A_PATH, [], function(er, data){
    //callback here
  });
  npm.on("log", function (msg) {
    console.log(msg + '');
  });
});
Tim
  • 41
  • 2
  • 10
  • 1
    As for 2022 this solution doesn't work anymore, npm >8.0.0 deprecated programmatic API, therefore `var npm = require("npm");` inside the project is no longer possible. – Sorin GFS Jun 24 '22 at 07:52