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.