2

I have been breaking my head over this. I have looked at a lot of tutorials, not sure what am I doing wrong. Here's my sample test.ts

import * as yargs from 'yargs';

const argv = yargs.options({
    env: {
        alias: 'e',
        choices: ['dev', 'prod'] as const,
        demandOption: true,
        description: 'app environment'
    },
    port: {
        alias: 'p',
        default: 80,
        description: 'port'
    }
  })
    .argv;

console.log(argv);

package.json

"dependencies": {
    "@types/node": "^16.9.1",
    "@types/yargs": "^15.0.0",
    "ts-node": "^10.7.0",
    "typescript": "^4.6.3",
    "winston": "^3.3.3",
    "yargs": "^15.4.1"
  },

Output

(base) 3c22fb37c8c169:test sriabhil$ npm run sri --env=prod --port=88

> test@1.0.0 sri
> ts-node ./src/test.ts

Options:
  --help      Show help                                                [boolean]
  --version   Show version number                                      [boolean]
  --env, -e   app environment                [required] [choices: "dev", "prod"]
  --port, -p  port                                                 [default: 80]

Missing required argument: env

Can someone please help me with this? I feel like I am missing something very basic but not sure what exactly

Abhilasha
  • 357
  • 1
  • 5
  • 19

1 Answers1

1

I realise how stupid I have been. I wasn't passing the arguments to npm script correctly. Changing the command from

npm run sri --env=prod --port=88

to

npm run sri -- --env=prod --port=88

fixed the issue

Abhilasha
  • 357
  • 1
  • 5
  • 19