How to use npm.commands.version to bump the package.json version programatically

I'd like the package.json version of my project to bump up every time I run my project, for that I installed the npm package into my project as a local node-module but the following does not work:

var npm = require('npm');
npm.load(function (e, n) {
    n.commands.version("patch", function (error) { 
        // 'error' here equals an error message as shown below
        console.log(error);
    });
});

Error message "npm version [ | major | minor | patch | prerelease | preminor | premajor ](run in package dir)'npm -v' or 'npm --version' to print npm version (2.4.1)'npm view version' to view a package's published version'npm ls' to inspect current package/dependency versions"

Any idea what's missing and how to make it work?

Thanks!

Btw, I don't use grunt and not planning to at this point, so grunt-bump is not an option.


Well when I read the docs, I see "Like all other commands, this function takes a string array as its first parameter", which means you need ["patch"] where you have "patch" :

var npm = require('npm');
npm.load(function (e, n) {
    n.commands.version(["patch"], function (error) {
        // 'error' here equals an error message as shown below
        console.log(error);
    });
});

That works fine for me.

链接地址: http://www.djcxy.com/p/27682.html

上一篇: 如何在npm安装过程中将分叉的repo更新为依赖项?

下一篇: 如何使用npm.commands.version以编程方式冲击package.json版本