Commander.js: typescript commander program.version is not a function

Created on 22 Dec 2018  路  5Comments  路  Source: tj/commander.js

I am getting following error while using commander with typescript.

TypeError: program.version is not a function
TypeError: program.option is not a function

index.ts

import * as program from 'commander';
program
.version('1.0.0')
    .option('-p, --peppers', 'Add peppers')
    .option('-P, --pineapple', 'Add pineapple')
    .option('-b, --bbq', 'Add bbq sauce')
    .parse(process.argv);
    console.log(program);

package.json

{
  "name": "typescript",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^10.12.18",
    "commander": "^2.19.0"
  }
}

All 5 comments

Hi, I have the same issue! What is the resolution for this problem ?

You can use it that way:

import { Command } from 'commander'; 
const command = new Command();

command
  .version('0.1.0')
  .option('-p, --peppers', 'Add peppers')
  .option('-P, --pineapple', 'Add pineapple')
  .option('-b, --bbq-sauce', 'Add bbq sauce')
  .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
  .parse(process.argv);

command.help();

> Usage: index.ts [options]
> 
> Options:
>   -V, --version        output the version number
>   -p, --peppers        Add peppers
>   -P, --pineapple      Add pineapple
>   -b, --bbq-sauce      Add bbq sauce
>   -c, --cheese [type]  Add the specified type of cheese [marble] (default: "marble")
>   -h, --help           output usage information

Would be nice to add typescript documentation.

Hi, I just got the same problem solved

@Toub your sollution works, but if you have your program split to more then one file, you will get different instances in each file.

@Zikoel @akki005 just try import synax without asterisk, it works for me with Node 10.15.1 and Commander 2.20.0

import program from 'commander';
// instead of
import * as program from 'commander';

Strange, on one project it works, and on other one i have this error:

sm.ts:3:8 - error TS1192: Module '"../node_modules/commander/typings/index"' has no default export.

3 import program from 'commander';

And I have no idea why, the versions are the same

Ok, I found the difference, in order to make it working you have to set "esModuleInterop": true, in tsconfig.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": ["es6", "es2015", "dom"],
    "declaration": true,
    "types": ["node"],
    "esModuleInterop": true,
  },
  "include": ["**/*.ts"]
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

shadowspawn picture shadowspawn  路  5Comments

youurayy picture youurayy  路  5Comments

FerencH picture FerencH  路  4Comments

0505gonzalez picture 0505gonzalez  路  3Comments

shadowspawn picture shadowspawn  路  3Comments