从grunt中更新NPM模块

我试图自动更新新的节点模块,但是,npm更新似乎不想从grunt中正确运行也更新package.json文件与新版本。 我想这样做,不pipe在package.json文件中指定了什么版本。

直到现在我发现的是节点模块:npm-check-updates( https://www.npmjs.com/package/npm-check-updates )

问题是,我不能让它与其他模块,如npm-shell或npm-exec。

我试过直接使用npm update -D,但是也失败了。

我问是否可以做。

以下是我使用的:

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(); }); }); 

如果我是正确的,你试图更新你的package.json文件中的所有npm包? 我会build议使用这个软件包。

安装软件包。

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

把它添加到你的咕噜任务。

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

然后将configuration添加到您的gruntfile.js

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

这里是参考:

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

之后你已经更新了包装更新你的devDependencies和依赖自动与一个咕task任务。 安装npm模块来更新你的开发依赖对象中的包。

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

把它添加到你的咕噜任务。

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

将你的configuration添加到你的grunt文件。

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

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

我使用npm-check-update(ncu)和grunt.util.spawnfind了一个解决scheme:

 // Install NPM Updates grunt.registerTask('update-npm', 'Update package.json and update npm modules', function() { grunt.log.writeln('If you get an error here, run "npm install -g npm-check-updates".'); grunt.task.run('npm-write-new'); grunt.task.run('npm-update'); }); // Check for npm module updates grunt.registerTask('npm-check', 'Check for npm modules updates', function() { var done = this.async(); grunt.log.writeln('Checking for npm modules updates ...'); grunt.util.spawn({ cmd: 'ncu', args: '', opts: { stdio: 'inherit', } }, function () { grunt.log.writeln('No files were modified.'); done(); }); }); // Write new versions to packages.json grunt.registerTask('npm-write-new', 'Write new versions to package.json', function() { var done = this.async(); grunt.log.writeln('Checking for npm modules updates ...'); grunt.util.spawn({ cmd: 'ncu', args: ['-u'], opts: { stdio: 'inherit', } }, function () { grunt.log.writeln('New versions were written to "package.json".'); done(); }); }); // Update npm modules grunt.registerTask('npm-update', 'Update npm modules', function() { var done = this.async(); grunt.log.writeln('Installing npm modules updates ...'); grunt.util.spawn({ cmd: 'npm', args: ['update','--loglevel','warn'], opts: { stdio: 'inherit', } }, function () { grunt.log.writeln('NPM modules were updated.'); done(); }); });