我有一个要部署到 Nodejitsu 的应用程序。最近,他们遇到了 npm 问题,导致我的应用程序在我尝试(但失败)重新启动它后离线几个小时,因为它的依赖项无法安装。有人告诉我,将来可以通过在 package.json 中将所有依赖项列为 bundledDependencies
来避免这种情况,从而使依赖项与应用程序的其余部分一起上传。这意味着我需要我的 package.json 看起来像这样:
"dependencies": {
"express": "2.5.8",
"mongoose": "2.5.9",
"stylus": "0.24.0"
},
"bundledDependencies": [
"express",
"mongoose",
"stylus"
]
现在,从 DRY 的角度来看,这并不吸引人。但更糟糕的是维护:每次添加或删除依赖项时,我都必须在两个地方进行更改。有没有可以用来同步 bundledDependencies
和 dependencies
的命令?
最佳答案
如何实现 grunt.js任务要做吗?这有效:
module.exports = function(grunt) {
grunt.registerTask('bundle', 'A task that bundles all dependencies.', function () {
// "package" is a reserved word so it's abbreviated to "pkg"
var pkg = grunt.file.readJSON('./package.json');
// set the bundled dependencies to the keys of the dependencies property
pkg.bundledDependencies = Object.keys(pkg.dependencies);
// write back to package.json and indent with two spaces
grunt.file.write('./package.json', JSON.stringify(pkg, undefined, ' '));
});
};
将它放在项目的根目录中的一个名为 grunt.js
的文件中。要安装 grunt,请使用 npm:npm install -g grunt
。然后通过执行 grunt bundle
来打包包。
一位评论员提到了一个可能有用的 npm 模块:https://www.npmjs.com/package/grunt-bundled-dependencies (我没试过。)
关于node.js - 有没有办法自动生成 bundledDependencies 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9652017/