Reputation: 2127
So, I am building an ElectronJS app for developers. This will check if NodeJS is installed on the computer. If not it will download and install NodeJS latest version and finally proceed with the app installation.
Also during installation of the electron app, I want to check, download and install a few node_modules globally.
This is to ensure that the user has all the tools available for initial installation and does not need to install them separately.
How do I achieve this?
Please make sure this process is cross-platform (Windows, Linux, and MAC)
Upvotes: 1
Views: 270
Reputation: 642
There are multiple ways to achieve what you want.
For one, you could spawn a child process from Node which just runs the npm command installing the required packages.
Another way is using npm programmatically. An example would look like this:
var npm = require('npm');
npm.load({ 'global': true }, function (err) {
if (err) console.log(err);
npm.commands.install(['hello-world'], function (err, data) {
if (err) return console.error(err)
});
});
This is not really recommended, since there is no real support for the programmatic API.
Last but not least, there are packages that also handle npm programmatically for you, like npm-programmatic, which also just spawns a child process in the background, but gives an easier interface for users/developers.
Upvotes: 1