Zeno Popovici Zeno Popovici - 2 years ago 89
Node.js Question

Update NPM modules from within grunt

I'm trying to automate the update of new node modules but, npm update seems not to want to run correctly from within grunt also updating the package.json file with the new version. I want to do this regardless of what version is specified in the package.json file.

What I found till now is the node module: npm-check-updates (https://www.npmjs.com/package/npm-check-updates)

The problem is that I can't get it to work with other modules like npm-shell or npm-exec.

I've tried using npm update -D directly, but that fails too.

I'm asking if it can be done.

Here's what I use:

grunt.registerTask('update', 'Update npm modules', function() {
var exec = require('child_process').exec;
var cb = this.async();
exec('npm update -D', {}, function(err, stdout) {
console.log(stdout);
cb();
});
});

Answer Source

If i'm correct you're trying to update ALL of your npm packages inside of your package.json file? I would recommend using this package.

Install the package.

npm install grunt-auto-install --save-dev

Add it to your grunt tasks.

grunt.loadNpmTasks('grunt-auto-install');

Then add the configuration to your gruntfile.js

grunt.initConfig({
  auto_install: {
    local: {},
    subdir: {
      options: {
        cwd: 'subdir',
        stdout: true,
        stderr: true,
        failOnError: true,
        npm: '--production'
      }
    }
  },
});

Here is the reference:

https://www.npmjs.com/package/grunt-auto-install

AFTER YOUVE UPDATED THE PACKAGES Update your devDependencies and dependencies automatically with a grunt task. Install the npm module to update your packages in your dev dependencies object.

npm install --save-dev grunt-dev-update

Add it to your grunt tasks.

grunt.loadNpmTasks('grunt-dev-update');

Add your configuration to your gruntfile.

devUpdate: {
        main: {
            options: {
                //task options go here 
            }
        }
    }

Reference: https://www.npmjs.com/package/grunt-dev-update

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download