使用" npm install"的问题编程

时间:2015-09-18 17:23:59

标签: javascript node.js npm

这是一个很棒的功能,我们可以使用" npm以编程方式,"但是我遇到了一些问题。功能" npm.load"似乎没有解雇。我没有得到我的" npm.load"中的任何控制台日志。或" npm.commands.install"功能

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.

任何帮助将不胜感激。如果您有任何问题,请与我们联系。

谢谢,

1 个答案:

答案 0 :(得分:0)

我花了几天时间,但我想了很多。

  1. 当我正在研究这个问题时,似乎已从npmjs.com中移除了documentation for using npm programmatically。不确定是否意味着他们弃用了模块,但在我发现文档被删除后我决定使用“child_process”。
  2. 当我在上面说过“npm.load”和“npm.install”没有被触发时,原因是我的节点应用程序运行了npm“nodemon”。每次我运行“加载”或“安装”nodemon都会认为这是对目录的更改,我的应用程序将重新启动。我也遇到了与“child_process”相同的问题。真傻!我知道了!
  3. 使用下面提供的解决方案,npm安装需要一段时间才能以编程方式运行,因此请做好相应的计划。
  4. 这是我提出的解决方案,它是承诺:

    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
    

    如果您有任何问题,请与我们联系。