How can I save all the installed node modules in package.json?

How can I save all the installed node modules in package.json without reinstalling them?

I have something like npm init --yes but, I'm not kinda sure if that works.

Thanks for the help!

Upvotes: 0

Views: 544

Answers (1)

abdulbari
abdulbari

Reputation: 6232

I think there is no way to get that stuff with some inbuilt modules

But you can write your own code to get that info and update in in your own package.json file

var fs = require("fs");

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

  });
}

getPackageInfo();

Output

"setprototypeof": "1.0.1",
"raw-body": "2.1.7",
"source-map": "0.4.4",
"statuses": "1.3.0",
"transformers": "2.1.0",
"type-is": "1.6.13",
"methods": "1.1.2",
"uglify-js": "2.7.3",
"uglify-to-browserify": "1.0.2",
"utils-merge": "1.0.0",
"unpipe": "1.0.0",
"vary": "1.0.1",
"void-elements": "2.0.1",
"with": "4.0.3",
"window-size": "0.1.0",
"wordwrap": "0.0.3",
"yargs": "3.10.0",
"mime-db": "1.24.0",
...................
..................
..................
.................

You can also use

npm list --depth=0

command to get packages list and version by child_process spawn

Upvotes: 1

Related Questions