【问题标题】:How do I save all the dependencies I install through npm into my package.json file?如何将通过 npm 安装的所有依赖项保存到我的 package.json 文件中?
【发布时间】:2017-02-02 03:29:54
【问题描述】:

我为很多包运行了npm install,但我忘记包含--save 参数。现在,当我尝试在 Heroku 上部署时,由于缺少某些依赖项而出现错误。我怎样才能自动将这些依赖项添加到我的package.json 文件中,而无需为每个依赖项都执行npm install --save?

【问题讨论】:

    标签: json node.js heroku npm


    【解决方案1】:

    您可以通过调用npm init 自动将所有未使用--save 安装的已安装包添加到您的package.json。它将依赖项附加到您现有的依赖项。文件中的任何设置都不应丢失。仍然不要忘记备份文件以确保 100% 安全!

    如果没有附加依赖,可能只是合并失败:

    1. 备份您现有的package.json,以便保留您在package.json 中已有的依赖项以及所有其他设置。我们稍后再需要这个文件。

    2. 删除 package.json 和 运行 npm init 以创建新的package.json,包括在@987654330 中未安装--save 的模块@。

    3. 合并新创建的package.json 的依赖项手动合并到旧的依赖项中。恢复您合并的package.json。

    【讨论】:

    • 这不会替换我的项目文件吗?
    • 您的项目文件和 node_modules 不会被此触及。这都是关于 package.json
    【解决方案2】:

    已经有人为此编写了脚本。 转到以下链接

    stackoverflow link

    这里是完整的代码 在您的项目文件夹中运行此代码

      var fs = require("fs");
    
      function main() {
        fs.readdir("./node_modules", function (err, dirs) {
          if (err) {
            console.log(err);
            return;
          }
          dirs.forEach(function(dir){
            if (dir.indexOf(".") !== 0) {
              var packageJsonFile = "./node_modules/" + dir + "/package.json";
              if (fs.existsSync(packageJsonFile)) {
                fs.readFile(packageJsonFile, function (err, data) {
                  if (err) {
                    console.log(err);
                  }
                  else {
                    var json = JSON.parse(data);
                    console.log('"'+json.name+'": "' + json.version + '",');
                  }
                });
              }
            }
          });
    
        });
      }
      main();
    

    它将打印 node_module 文件夹中的所有依赖项,如下所示。

    "ansi-regex": "2.0.0",
    "ansi-styles": "2.2.1",
    "asn1": "0.2.3",
    "assert-plus": "0.2.0",
    "asynckit": "0.4.0",
    "aws-sign2": "0.6.0",
    "bcrypt-pbkdf": "1.0.0",
    "aws4": "1.4.1",
    "bindings": "1.2.1",
    "bl": "1.1.2",
    "boom": "2.10.1",
    "caseless": "0.11.0",
    "chalk": "1.1.3",
    "combined-stream": "1.0.5",
    "core-util-is": "1.0.2",
    "compress": "0.99.0",
    "commander": "2.9.0",
    "cryptiles": "2.0.5",
    "delayed-stream": "1.0.0",
    "dashdash": "1.14.0",
    "debug": "0.7.4",
    "ecc-jsbn": "0.1.1",
    "ejs": "2.3.4",
    "escape-string-regexp": "1.0.5",
    

    复制并粘贴到您的 package.json json 中,如下所示

    {
      "name": "test",
      "version": "1.0.0",
      "main": "server.js",
      "dependencies": {
        //paste above printed data here
      },
      "devDependencies": {},
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "description": ""
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 2014-06-06
      • 2017-04-18
      • 2013-01-17
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 2015-12-17
      相关资源
      最近更新 更多