简体   繁体   中英

How do I save all the dependencies I install through npm into my package.json file?

I ran npm install for a lot of packages, but I forgot to include the --save argument. Now when I try to deploy on Heroku I get errors for missing certain dependencies. How can I automatically add those dependencies to my package.json file without doing npm install --save for each one?

You can add all installed packages not installed with --save to your package.json automatically by calling npm init . It will append the dependencies to your existing ones. No settings in your file should be lost. Still don't forget to make a backup of the file to be 100% secure!

If the dependencies have not been appended, it can happen that just the merging failed:

  1. Backup your existing package.json in order to keep the dependencies you have in your package.json already and all the other settings. We need this file later again.

  2. Delete the package.json and run npm init in order to create a new package.json including the modules installed without --save in dependencies .

  3. Merge the dependencies of your newly created package.json into your old one manually. Restore your merged package.json .

Someone already wrote a script for this. Go to following link

stackoverflow link

here is complete code run this code inside your project folder

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

It will print all the dependencies inside node_module folder as given below.

"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",

copy and paste inside your package.json json as follow

{
  "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": ""
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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