简体   繁体   中英

Issues with using “npm install” programmatically

It is an awesome feature that we can use "npm programmatically," but I am running into some issues. The function "npm.load" does not seems to be firing. I am not getting any console logs that are inside of my "npm.load" or "npm.commands.install" functions.

var npm = require('npm');

// There is another promise here
.then(function(path) {

  // this is working the way I intend it to
  cache[requestId].package = JSON.parse(path);

  // This is firing
  if (cache[requestId].package.name && cache[requestId].package.version && cache[requestId].package.scripts.start) {

    // console logs and an array [ 'keystone', 'async', 'underscore', 'swig', 'node-sass', 'node-sass-middleware', 'dotenv' ]
    console.log(Object.keys(cache[requestId].package.dependencies));

    // console logs as /Users/207004/Desktop/github/mothership/server/app/routes/tractor-beam/ms-apps/my_site
    console.log(localPath);

    // console logs as a [Function]
    console.log(npm.load);

    // *** Here is the issue! This is not firing! ***
    npm.load({}, function(err) {

      // no console log
      console.log(npm.commands.install);

      // no console log
      console.log(err);
      npm.commands.install(localPath, Object.keys(cache[requestId].package.dependencies), function(err, done) {

        // no console log
        console.log('loaded');

        // no console log
        console.log(err, done);

        // I am assuming that this is not firing, but my code does fire the console log in the next promise
        return PM2.connectAsync();
      });
    });
  } else {
    console.log('else');
  }
})
// Another promise chained here. A console log inside of this promise is firing.

Any help would be appreciated. Please let me know if you have any questions.

Thanks,

It took me a few days, but I figured a lot out.

  1. While I was working on this, it seems that the documentation for using npm programmatically was removed from npmjs.com. Not sure if it means they deprecated the module, but I decided to use "child_process" after I found that the documentation was removed.
  2. When I stated above that "npm.load" and "npm.install" was not firing, the reason was that I had my node app running with the npm "nodemon." Every time I would run "load" or "install" nodemon would consider this a change to the directory and my app would restart. I ran into the same issue with "child_process" as well. Really dumb! I know!
  3. With my solution provided below, it takes npm install a while to run programmatically, so plan accordingly.

Here is the solution I came up with and it's with promises:

var Promise = require('bluebird');

// This will create promise functions for all the methods in the "child_process" module.
// Created "exec.execAsync" below.
var exec = Promise.promisifyAll(require('child_process'));

// Function to make package names one long string for the command line.
var getDependencies = function(dependencies) {
  var deps = '';

  Object.keys(dependencies).forEach(function(el){
    deps = deps + ' ' + el;
  });

  return deps;
};

// Promise before this reads the package.json file
.then(function(packageJson){
  var deps;
  var pack = JSON.parse(packageJson);
    if(pack && pack.dependencies) {
      deps = getDependencies(pack.dependencies);

      // I used the "--prefix" flag because I wanted to install the dependencies in a different directory.
      // This part takes a while. Plan your promises before and after accordingly.
      // The command below console logs into this "npm install --prefix /Users/Max/Desktop/github/mothership/server/app/routes/tractor-beam/ms-apps/my_site keystone async underscore swig node-sass node-sass-middleware dotenv"
      return exec.execAsync('npm install --prefix ' + localPath + deps);
    }
  })
// Continues to next promise

Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM