My goal is to develop an npm TypeScript module that serves dual purposes - acting as a command line utility and exporting methods for use in other modules.
The issue arises when the module intended for use as a command line utility requires a node shebang (#!/usr/bin/env node) in the first line of index.ts. Importing this module into another one causes its code to start executing before any exported method is actually called. Here's an example:
#!/usr/bin/env node
const arg1: number = parseFloat(process.argv[2]);
const arg2: number = parseFloat(process.argv[3]);
console.log(superCalc(arg1, arg2)); // this gets called when superCalc() is referenced in another module
export function superCalc(arg1: number, arg2: number): number {
return arg1 + arg2;
}