node.js - 有没有办法自动生成 bundledDependencies 列表?

标签 node.js npm nodejitsu

我有一个要部署到 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 的角度来看,这并不吸引人。但更糟糕的是维护:每次添加或删除依赖项时,我都必须在两个地方进行更改。有没有可以用来同步 bundledDependenciesdependencies 的命令?

最佳答案

如何实现 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/

相关文章:

javascript - 使用 fs.readFileSync() 提供 html 文件失败

javascript - 我正在尝试将 pdf 文件从 Node 服务器下载到 React 客户端,但是当我打开它时,它显示空白

node.js - 错误消息 npm-default 不可用

node.js - 将私有(private)存储库部署为依赖项

node.js - 使用 libxmljs 和 nodejs 时出现无效的 XSD 架构

node.js - 使用反向代理 Nginx 服务 React 创建应用程序和 Nodejs 应用程序

vue.js - 如何修复 Visual Studio Code 中的 "Error: Watching remote files is not supported"问题

node.js - Node 包安装失败,因为 Node 在我的根目录中查找依赖项

node.js - 我应该使用来自服务提供商或heroku 插件的elasticsearch 还是我自己设置的elasticsearch?

javascript - NodeJS 的高延迟