typescript commander program.version is not a function

See original GitHub issue

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"
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

2reactions
myflowplcommented, May 6, 2019

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"]
}
2reactions
myflowplcommented, May 6, 2019

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';
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use commander in typescript - node.js - Stack Overflow
With your first code snippet and the following dependencies, I do not get an error: "dependencies": { "commander": "^2.11.0" } ...
Read more >
commander - npm
Version option​​ The default option flags are -V and --version , and when present the command prints the version number and exits. You...
Read more >
Building a TypeScript CLI with Node.js and Commander
Commander.js is a powerful library for building command-line interface tools with TypeScript. Learn how to use Commander with TypeScript.
Read more >
How to set up TypeScript
You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal. To do...
Read more >
How to use the commander.version function in ... - Snyk
/usr/bin/env node var program = require('commander'); var cli = require('./app/cli.js'); program .version('0.0.1'); // .description('Google Cloud Functions ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found