How to install npm package from nodejs script? - node.js

How to install npm package from nodejs script?
Question is not about simple installation npm packages via terminal,
it is about installation via nodejs script:
Not about this: npm install express, but about having install.js file with content npm install express, which I will execute like node install.js and after this it will locally install express module in this folder.
Sorry, but Google and DuckDuckGo are not my friends today(
The main problem is in automatic local installation required packages for my small utility, because global packages are not working in windows.

Check out commander.js it allows you to write command line apps using node.
Then you can use the exec module.
Assuming you put the following in install.js, you just have to do: ./install.js and it will run npm install for you.
#!/usr/bin/env node
var program = require('commander');
var exec = require('child_process').exec;
var run = function(cmd){
var child = exec(cmd, function (error, stdout, stderr) {
if (stderr !== null) {
console.log('' + stderr);
}
if (stdout !== null) {
console.log('' + stdout);
}
if (error !== null) {
console.log('' + error);
}
});
};
program
.version('0.1.3')
.option('i, --install ', 'install packages')
.parse(process.argv);
if (program.install) {
run('npm install');
}
var count = 0;
// If parameter is missing or not supported, display help
program.options.filter(function (option) {
if(!(option.short == process.argv[2]))
count++
});
if(count == program.options.length)
program.help();
Hope this helps!

NOTE: I don't think this fulfills all the requirements of your question, because at the end you state that you can't find npm...so maybe your question would be better titled "How to install npm package without npm?"--yikes! But it addresses the title, "How to install npm package from nodejs script?"
I've just been shown another alternative to do this: the module npmi. While this is still another module dependency, it does at least work without a *nix shell script environment, which I think the other answer here (about commander.js) does. And, if you look inside the code for npmi.js, you'll find it's very short and merely uses the npm module directly in the node script--which you can do yourself if you don't want to add the npmi module.
So in our case we needed a way to install modules without requiring a *nix shell script (to support Windows users), and this fits the bill nicely.
That still doesn't help you if you can't require('npm'). Only thing I can think of there is trying likely absolute paths...you can require('C:\Program Files\Node\packages\x)`, I think--or wherever node's global packages are stored (per user?). Wrap a couple of attempts in try/catch or test for the file's existence first and try to require the npm module whenever you find where the global packages are actually installed? You might tick off a malware scanner :-), but it might work.

Related

Electron js: use npm internally/programmatically

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 ?
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 + '');
});
});

Node temporary npm install done programmatically

I would like to programmatically npm install a package using a Node app, after the node app has started. Ideally, this package would not file into my node_modules folder, but rather would trash itself after runtime.
npm supports programmatic installs, however it seems to actually save the modules into node_modules. Additionally, making the entirety of npm (a big module) a requirement for this kind of sucks. However, when I looked at the source code, the npm install part uses a ton of modules and isn't something I can easily reproduce.
Is there any other module that anyone knows about that meets this requirement?
Found with NPM you can install to a path, and there's this nifty temp module that helps do that cross platform:
var temp = require('temp')
, npm = require('npm')
;
function use(module, cb) {
npm.load({}, function(){
npm.commands.install(temp.dir, [module], function(err, data){
var dir = data[0][1];
var mod = require(__dirname + '/' + dir);
cb(mod);
});
})
}
use('lodash', function(_){
// ... do things.
});
If you want to, temp has a clean function that can clean up the temp dir later.

Node.js, Bash Scripts and Directory-local node binary

In our environment, we're building a build/util tool to be used by many users without the need for external (or system-wide) dependencies. Namely, we're attempting to build this so that users won't have to be concerned with which version of node.js they have installed globally, as we're packing the node.js binary within the tool directory. (Please refrain from commenting on the merits of such an approach).
Our tool structure is as such:
/web/tool/
/web/tool/bin/ (where the node binary lives)
/web/tool/node_modules
We have /web/tool/bin added to the $PATH variable (first in order) upon execution of any of the scripts or grunt.js tasks to ensure that the local binaries path trumps any other.
Upon execution of any bash scripts or grunt.js tasks, the initial task locates the proper node binary. This was tested in an environment without node installed globally, so we knew it was finding the directory-local node binary. Any subsequent processes spawned from within these scripts/grunt-tasks however, are looking globally for the node binary.
Our specific user-case is running karma and phantomjs via a grunt task. The bash scripts/bins for karma, grunt-cli (grunt), and phantomjs all have the familiar header directive of
#!/usr/bin/env node
How should we go about setting up so that our tool is always looking for the directory-local node binary, even in subsequent child-processes?
Definitely try creating a wrapper script to set environment variables to pass on to child classes:
node ./wrapper.js (you write/execute this one)
#!/usr/bin/env node
console.log(process.env.NODE); // "/usr/local/bin/node"
process.env.NODE = "/web/tool/bin/node";
require('child_process').exec('node ./child.js', {
'cwd': __dirname,
'env': process.env
}, function(err, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (err !== null) {
console.log('exec error: ' + error);
}
});
child.js (or whatever other script you have, note that it does not need to be modified)
#!/usr/bin/env node
console.log(process.env.NODE);
Output:
$ node env.js
/usr/local/bin/node
stdout: /web/tool/bin/node (from child.js)
stderr:

Using nodejs's spawn causes "unknown option -- " and "[Error: spawn ENOENT]" errors

I'm trying to get spawn to effect an rm -rf node_modules followed by npm install (on windows 7; nx commands courtesy of a transparently installed CygWin. All nx commands resolve on a commandline just fine).
I initially had this using exec, but wanted to catch the stdout/stderr information as it occurred, so I figured I'd use spawn, and rewrote the code to use that. However, that breaks everything.
The rm command, rewritten, became this:
var spawn = require("child_process").spawn,
child = spawn("rm", ["-rf", "node_modules"]);
child.stdout.on('data', function (data) { console.log(data.toString()); });
child.stderr.on('data', function (data) { console.log(data.toString()); });
child.on('error', function() { console.log(arguments); });
However, running this generates the following error:
rm: unknown option -- ,
Try `rm --help' for more information.
The npm command, rewritten, became this:
var spawn = require("child_process").spawn,
child = spawn("npm", ["install"]);
child.stdout.on('data', function (data) { console.log(data.toString()); });
child.stderr.on('data', function (data) { console.log(data.toString()); });
child.on('error', function() { console.log(arguments); });
However, running this generates the following error:
{
'0': {
[Error: spawn ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn'
}
}
How do I make spawn run the same commands that worked fine using exec without it throwing up errors all over the place? And why does this not work? Reading the API, http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options, seems to suggest this is precisely how one is supposed to use spawn...
After lots of trying different things, I finally had a look at what "npm" actually is on windows, and it turns out to be a bash script called npm, as well as a windows-native batch script called npm.cmd (no idea why it's .cmd, that should be .bat, but there you have it). Windows's command resolver will see npm, notice that it's not an executable, see npm.cmd, and then notice that IS an executable, and will then use that instead. This is helpful when you're in a terminal, but spawn() will not do any such resolution: passing it npm will make it fail because it's not an executable. Passing it npm.cmd as command, however, works just fine.
(Also, not sure why rm was failing earlier, since that actually works correctly without any changes that I can tell. Probably misread that as part of the problem when in fact it wasn't.)
So: if you run into spawn saying ENOENT in windows, when the command you're trying to trigger works in a plain command prompt, find out if the command you're calling is a true executable, or whether there's a .bat/.cmd file that the command prompt will "helpfully" run for you instead. If so, spawn that.
edit
since this post is still getting upvotes, a good way to ensure the command always works is to bootstrap it based on process.platform, which will be win32 for windows.
var npm = (process.platform === "win32" ? "npm.cmd" : "npm"),
child = spawn(npm, ["install", ...]);
...
edit specific to the use-case that triggered this error
since posting this question (and its answer), several packages have been released that allow running npm tasks without having to rely on exec or spawn, and you should use them instead.
Probably the most popular is npm-run-all which doesn't just give you the power to run any npm task from other npm scripts as well as from Node, but also adds commands to run multiple npm scripts in series or in parallel, with or without wildcards.
In the context of the original question, where the error got thrown because I was trying to run npm as an exec/spawn in order to effect a cleanup and reinstall, the modern solution is to have a dedicated cleaning task in package.json:
{
...
"scripts": {
"clean": "rimraf ./node_modules",
...
},
...
}
And to then invoke that clean task followed by the install command on the command line as
> npm run clean && npm install
Or, from inside some Node script, using:
const runAll = require("npm-run-all");
...
runAll(["clean", "install"])
.then(() => {
console.log("done!");
})
.catch(err => {
console.log("failed!");
});
(Or of course, as a compound script in package.js, e.g. "redo": "run-s clean install" and then using runAll(["redo"]))
I think this may be some sort of cygwin gotcha. I'm running Ubuntu 12.04 and tried to duplicate your problem, but it works just fine for me. In short, I don't see anything you are doing wrong.
If it is complaining about the option, maybe split it up into multiple options like so:
child = spawn("rm", ["-r", "-f", "node_modules"]);
That's kind of a hail mary, but that works on my Ubuntu 12.04 as well. You might try to just delete a single file and see if you get the same thing.
child = spawn("rm", ["/home/username/Desktop/TestFile"]);
If that still fails, then you know you are working against some crazy stuff.
You could even try to just execute a command with no parameters like so:
child = spawn("ls");
If that still fails, you aren't likely to get spawn to work at all would be my guess and be grateful that at least exec is working.
Not much in the realm of answers for you, but like I said, I can't see anything you are doing incorrectly.
Furthermore, I don't see how your npm command is going to work because you aren't specifying what to install, but that being said, it fails in a different way than I'm seeing it fail here if I use the same command. . . I see lots of stderr output, not an overall error.
BTW, I'm running node v0.8.21. You can query that by node -v. If you are running another version, maybe give 0.8.21 a try.
Use full path for the process, like:
var cmd = require('child_process').spawn("C:\\windows\\system32\\cmd.exe");

Determine NPM modules used from a running node.js application

Other than grabbing the package.json file at the project root is there a way to determine the list of dependencies of a running node.js application? Does node keep this meta information available as some var in the global namespace?
If you are just looking for the currently installed npm packages in the application directory, then you can install the npm package (npm install -g npm) and programatically invoke ls to list the installed packages and the dependency trees.
Obviously, this has no bearing on whether the installed packages are actually require'd in the application or not.
Usage is not that well documented but this should get you started.
var npm = require('npm');
npm.load(function(err, npm) {
npm.commands.ls([], true, function(err, data, lite) {
console.log(data); //or lite for simplified output
});
});
e.g.:
{ dependencies:
{ npm: { version: '1.1.18', dependencies: [Object] },
request: { version: '2.9.202' } } }
Otherwise, I believe the only other option is to introspect the module module to get information pertaining to the currently loaded/cached module paths. However this definitely does not look to have been developed as a public API. I'm not sure if there are any alternatives so would be keen to hear if there are e.g.
var req = require('request'); // require some module for demo purposes
var m = require('module');
// properties of m contain current loaded module info, e.g. m._cache
I believe you could use require-analyzer, which sort of works according to Isaacs(could miss some). You could hear this in Nodeup's first podcast from 11:55.
Or you could try James node-detective which probably will find your dependencies better(but not by running code), but because of Javascript dynamic nature(12:46).
detective
Find all calls to require() no matter how crazily nested using a
proper walk of the AST.
P.S: to expose those package.json variables to node.js you could use node-pkginfo

Resources