简体   繁体   English

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

[英]Grunt and NPM, package all production dependencies

I am unsure when the way the NPM installs dependencies changed. 我不确定NPM安装依赖项的方式何时更改。
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: 过去我记得,如果在我的project.json我依赖于“ abc”,而后者又依赖于“ xyz”,那么npm install会导致类似以下内容:

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). 在打包要由AWS Lambda使用的节点项目时,我必须包括该node_modules结构(减去那里的任何开发依赖性)。 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): 我将使用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). 最近(或者可能不是最近),这种情况发生了变化(我不确定何时使用非常老版本的NPM并对其进行了更新)。
Now if I depend on "abc" which in turn depends on "xyz" I will get: 现在,如果我依赖于“ 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? 有什么简单的方法来获取grunt作业中的生产依赖项列表(以及子依赖项)?
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... 我可以使用递归函数扫描来查找我的依赖项,然后检查那些依赖项的project.json文件,然后搜索子依赖项等。这样做似乎很麻烦,这可能是许多项目的常见情况...

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); }); }; 

This change was introduced with the release of npm 3 (see npm v3 Dependency Resolution ). 此更改是在npm 3发行版中引入的(请参阅npm v3 Dependency Resolution )。

It's not exactly clear why you need to use Grunt at all. 目前还不清楚为什么需要使用Grunt。 If what you want to do is get only production dependencies you can simply run: 如果您只想获取生产依赖项,则可以简单地运行:

npm install --production

With the --production flag, all dev dependencies will be ignored. 使用--production标志,将忽略所有开发依赖项。 The same is also true if the NODE_ENV environment variable is set to 'production' . 如果将环境变量NODE_ENV设置为'production',则也是如此。

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

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