How to save all the dependencies that I install via npm to the package.json file?

I ran npm installfor a lot of packages, but I forgot to include the argument --save. Now when I try to deploy to Heroku, I get errors for the absence of certain dependencies. How can I automatically add these dependencies to my file package.jsonwithout doing npm install --savefor each of them?

+4
source share
2 answers

You can add all installed packages that are not installed with --saveto yours package.jsonautomatically by calling npm init. It will add dependencies to your existing ones. No settings in your file should be lost. However, do not forget to make the backup file 100% safe!

If dependencies have not been added, it may happen that just a merge failed:

  • Backing up an existing one package.jsonto save dependencies in you package.jsonalready and in all other settings. We need this file later.

  • Uninstall package.json and run npm init to create a new one package.json, including modules installed without --savein dependencies.

  • package.json . package.json.

+6

- script.

fooobar.com/questions/19266/...

  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": ""
}
+1

Source: https://habr.com/ru/post/1655687/


All Articles