Krishnaveni
Krishnaveni

Reputation: 809

How to update node modules programmatically

I need to use the npm update from a script. Below is my code:

var npm = require('npm');
npm.load(function () {
npm.commands.outdated({json: true}, function (err, data) {
    //console.log(data);
    npm.commands.update(function(err, d){
        console.log(d);
    });
   });
});

When I run this script, the modules get updated, but the new versions are not indicated in the package.json.

When I run npm update --save-dev from command line, folders and package.json get updated.

Please suggest how this can be achieved through the script. How can I use --save-dev option through code?

Upvotes: 6

Views: 1388

Answers (2)

gnerkus
gnerkus

Reputation: 12019

You'll need to specify the {save: true} option when loading the config:

npm.load({save: true}, function() {
  // update code
});

Edit:

The 'save-dev' option for the npm.load command does not work. There's an issue about it here: https://github.com/npm/npm/issues/2369.

The work around is to re-install outdated modules:

npm.load({'save-dev': true}, function () {
  npm.commands.outdated(function (err, rawOutdated) {
    var outdated = rawOutdated.map(function (module) {
      return module[1];
    });
    npm.commands.install(outdated, function(err, d) {

    });
   });
});

Upvotes: 2

filype
filype

Reputation: 8380

I think the first argument in the npm.commands.update is a list of arguments. I have never used npm programmatically, but looking at their source code I would try the following:

npm.commands.update(['--save-dev'], function(err, d){
    console.log(d);
});

The reference is on this test: https://github.com/npm/npm/blob/master/test/tap/update-save.js#L87

Upvotes: 2

Related Questions