Grunt和NPM,打包所有生产依赖项

时间:2023-01-19 18:15:22

I am unsure when the way the NPM installs dependencies changed.
In the past I remember that if in my project.json I had a dependency on "abc", which in turn would depend on "xyz", a npm install would result in something like:

我不确定NPM安装依赖项的方式何时发生变化。在过去,我记得如果在我的project.json中我依赖于“abc”,而这依赖于“xyz”,那么npm安装会产生类似于:

package.json
node_modules/
    abc/
        node_modules/
            xyz/
    some-dev-dep/

When packaging my node project to be used by AWS Lambda, I would have to include that node_modules structure (less any dev-dependencies that were there). I would use Grunt for my packaging, so I wrote this handy thing to help me get all production dependencies into this zip (extracting part of my gruntfile.js):

打包我的节点项目以供AWS Lambda使用时,我必须包含该node_modules结构(减去那里的任何dev依赖项)。我会使用Grunt作为我的包装,所以我写了这个方便的东西来帮助我将所有生产依赖项放到这个zip中(提取我的gruntfile.js的一部分):

function getDependencies(pkg) {
    return Object.keys(pkg.dependencies)
        .map(function(val) { return val + '/**'; });
}

var config = {
    compress: {
        prod: {
            options: {
                archive: 'public/lambda.zip'
            },
            files: [
                { src: 'index.js', dest: '/' },
                { expand: true, cwd: 'node_modules/', src: getDependencies(pkg), dest: '/node_modules' }
            ]
        }
    }
};

This would work because dependencies of my dependencies were nested.
Recently (or maybe not-so-recently) this has changed (I am unsure when as I was using very old version of NPM and updated it recently).
Now if I depend on "abc" which in turn depends on "xyz" I will get:

这可以工作,因为我的依赖项的依赖项是嵌套的。最近(或者可能不是最近)这已经改变了(我不确定何时使用非常旧版本的NPM并最近更新它)。现在,如果我依赖“abc”,而“abc”依赖于“xyz”,我会得到:

node_modules/
    abc/
    xyz/
    some-dev-dep/

As you can see, my way of getting only production dependencies just won't work.
Is there any easy way to get only list of production dependencies (together with sub-dependencies) within grunt job?
I could do it using recursive function scanning for my dependencies, and then checking project.json files of those and then searching for sub-dependencies etc. This approach seems like a lot of hassle that is possibly a common scenario for many projects...

正如您所看到的,我只获取生产依赖关系的方式不起作用。有没有简单的方法来获取grunt作业中的生产依赖项列表(以及子依赖项)?我可以使用递归函数扫描我的依赖项,然后检查那些的project.json文件,然后搜索子依赖项等。这种方法似乎很麻烦,可能是许多项目的常见场景...

2 个解决方案

#1


1  

Here is a function that returns an array of the production dependency module names. (Note: you might need to have the 'npm' module installed locally in your project for this to work.)

这是一个返回生产依赖项模块名称数组的函数。 (注意:您可能需要在项目中本地安装'npm'模块才能使其正常工作。)

/**
 * Returns an array of the node dependencies needed for production.
 * See https://docs.npmjs.com/cli/ls for info on the 'npm ls' command.
*/
var getProdDependencies = function(callback) {
  require('child_process').exec('npm ls --prod=true --parseable=true', undefined,
      function(err, stdout, stderr) {
        var array = stdout.split('\n');
        var nodeModuleNames = [];

        array.forEach(function(line) {
          var index = line.indexOf('node_modules');
          if (index > -1) {
            nodeModuleNames.push(line.substr(index + 13));
          }
        });

        callback(nodeModuleNames);
      });
};

#2


0  

This change was introduced with the release of npm 3 (see npm v3 Dependency Resolution).

随着npm 3的发布引入了这一变化(参见npm v3依赖性解决方案)。

It's not exactly clear why you need to use Grunt at all. If what you want to do is get only production dependencies you can simply run:

目前还不清楚为什么你需要使用Grunt。如果你想要做的只是生成依赖,你可以简单地运行:

npm install --production

npm install --production

With the --production flag, all dev dependencies will be ignored. The same is also true if the NODE_ENV environment variable is set to 'production'.

使用--production标志,将忽略所有dev依赖项。如果NODE_ENV环境变量设置为“生产”,情况也是如此。

#1


1  

Here is a function that returns an array of the production dependency module names. (Note: you might need to have the 'npm' module installed locally in your project for this to work.)

这是一个返回生产依赖项模块名称数组的函数。 (注意:您可能需要在项目中本地安装'npm'模块才能使其正常工作。)

/**
 * Returns an array of the node dependencies needed for production.
 * See https://docs.npmjs.com/cli/ls for info on the 'npm ls' command.
*/
var getProdDependencies = function(callback) {
  require('child_process').exec('npm ls --prod=true --parseable=true', undefined,
      function(err, stdout, stderr) {
        var array = stdout.split('\n');
        var nodeModuleNames = [];

        array.forEach(function(line) {
          var index = line.indexOf('node_modules');
          if (index > -1) {
            nodeModuleNames.push(line.substr(index + 13));
          }
        });

        callback(nodeModuleNames);
      });
};

#2


0  

This change was introduced with the release of npm 3 (see npm v3 Dependency Resolution).

随着npm 3的发布引入了这一变化(参见npm v3依赖性解决方案)。

It's not exactly clear why you need to use Grunt at all. If what you want to do is get only production dependencies you can simply run:

目前还不清楚为什么你需要使用Grunt。如果你想要做的只是生成依赖,你可以简单地运行:

npm install --production

npm install --production

With the --production flag, all dev dependencies will be ignored. The same is also true if the NODE_ENV environment variable is set to 'production'.

使用--production标志,将忽略所有dev依赖项。如果NODE_ENV环境变量设置为“生产”,情况也是如此。