Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>NPM NodeJS API is not well documented, but checking the code helps up.</p> <p><a href="https://github.com/isaacs/npm/blob/a362c3f1919987419ed8a37c8defa19d2e6697b0/lib/install.js#L16-L29" rel="noreferrer">Here</a> we find the following string:</p> <pre><code>install.usage = "npm install" + "\nnpm install &lt;pkg&gt;" + "\nnpm install &lt;pkg&gt;@&lt;tag&gt;" + "\nnpm install &lt;pkg&gt;@&lt;version&gt;" + "\nnpm install &lt;pkg&gt;@&lt;version range&gt;" + "\nnpm install &lt;folder&gt;" + "\nnpm install &lt;tarball file&gt;" + "\nnpm install &lt;tarball url&gt;" + "\nnpm install &lt;git:// url&gt;" + "\nnpm install &lt;github username&gt;/&lt;github project&gt;" + "\n\nCan specify one or more: npm install ./foo.tgz bar@stable /some/folder" + "\nIf no argument is supplied and ./npm-shrinkwrap.json is " + "\npresent, installs dependencies specified in the shrinkwrap." + "\nOtherwise, installs dependencies from ./package.json." </code></pre> <p>My question is about the version, so we can do: <code>hello-world@0.0.1</code> to install <code>0.0.1</code> version of <code>hello-world</code>.</p> <pre><code>var npm = require("npm"); npm.load({ loaded: false }, function (err) { // catch errors npm.commands.install(["hello-world@0.0.1"], function (er, data) { // log the error or data }); npm.on("log", function (message) { // log the progress of the installation console.log(message); }); }); </code></pre> <p>I didn't test, but I am sure that we can use any format of the <code>install.usage</code> solutions.</p> <p>I wrote a function that converts the <code>dependencies</code> object in an array that can be passed to the <code>install</code> function call.</p> <p><code>dependencies:</code></p> <pre><code>{ "hello-world": "0.0.1" } </code></pre> <p>The function gets the path to the <code>package.json</code> file and returns an array of strings.</p> <pre><code>function createNpmDependenciesArray (packageFilePath) { var p = require(packageFilePath); if (!p.dependencies) return []; var deps = []; for (var mod in p.dependencies) { deps.push(mod + "@" + p.dependencies[mod]); } return deps; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload