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?
 
    
    - 3,408
- 6
- 30
- 53
2 Answers
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:
- Backup your existing - package.jsonin order to keep the dependencies you have in your- package.jsonalready and all the other settings. We need this file later again.
- Delete the - package.jsonand run- npm initin order to create a new- package.jsonincluding the modules installed without- --savein- dependencies.
- Merge the dependencies of your newly created - package.jsoninto your old one manually. Restore your merged- package.json.
 
    
    - 3,336
- 2
- 25
- 41
- 
                    won't this replace my project files? – Jonathan Allen Grant Sep 24 '16 at 12:21
- 
                    2Your project files and node_modules are not touched by this. It's all about the package.json – Michael Troger Sep 24 '16 at 12:30
Someone already wrote a script for this. Go to following 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": ""
}
 
    
    - 1
- 1
 
    
    - 417
- 2
- 15