简体   繁体   English

以编程方式使用“ npm install”的问题

[英]Issues with using “npm install” programmatically

It is an awesome feature that we can use "npm programmatically," but I am running into some issues. 我们可以通过编程方式使用“ npm”,这是一个了不起的功能,但是我遇到了一些问题。 The function "npm.load" does not seems to be firing. 函数“ npm.load”似乎没有触发。 I am not getting any console logs that are inside of my "npm.load" or "npm.commands.install" functions. 我没有在“ 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.

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. 在我进行此操作时,似乎已从npmjs.com中删除了以编程方式使用npm文档 Not sure if it means they deprecated the module, but I decided to use "child_process" after I found that the documentation was removed. 不知道这是否意味着他们不赞成使用该模块,但是在发现文档被删除后,我决定使用“ child_process”。
  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." 当我在上面说“ npm.load”和“ npm.install”没有启动时,原因是我的节点应用运行了npm“ nodemon”。 Every time I would run "load" or "install" nodemon would consider this a change to the directory and my app would restart. 每次我运行“加载”或“安装”时,nodemon都会认为这是对目录的更改,并且我的应用程序将重新启动。 I ran into the same issue with "child_process" as well. 我也遇到了与“ child_process”相同的问题。 Really dumb! 真傻! I know! 我知道!
  3. With my solution provided below, it takes npm install a while to run programmatically, so plan accordingly. 使用下面提供的解决方案,以npm安装需要花一些时间才能以编程方式运行,因此请进行相应的计划。

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. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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