Commander.js: Take -version from package.json

Created on 5 Feb 2012  路  5Comments  路  Source: tj/commander.js

E.g.

program.version(JSON.parse(require('fs').readFileSync(
     require.main.filename.match(/^(.+)\/.+$/)[1] + '/../package.json')).version)

But a test is needed for the existence of package.json, I think the 2 most used locations are and /.

So 1, check for main_module_dir/package.json and 2, check for main_module_dir/../package.json.

Most helpful comment

For others finding this issue: you know where the package.json is in your project, so you can just do:

program
    .version(require('./package.json').version)

All 5 comments

Agreed, I'd love this.

+1

just realized this is implemented wrong, you'll get commander's version every time

your require() is always relative to commander's ./lib

For others finding this issue: you know where the package.json is in your project, so you can just do:

program
    .version(require('./package.json').version)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

snitin315 picture snitin315  路  4Comments

mathiasbynens picture mathiasbynens  路  3Comments

mtrabelsi picture mtrabelsi  路  3Comments

shadowspawn picture shadowspawn  路  5Comments

RoXioTD picture RoXioTD  路  4Comments