Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config file not being loaded #2040

Closed
fallemand opened this issue Oct 7, 2021 · 4 comments
Closed

config file not being loaded #2040

fallemand opened this issue Oct 7, 2021 · 4 comments
Labels

Comments

@fallemand
Copy link

fallemand commented Oct 7, 2021

Hi, I have a simple program:

// my-program.js
#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv));
argv
  .scriptName("my-program")
  .config()
  .option("test", {
    "demandOption": true
  })
  .parse();

And a config.json file

{
  "test": "teeeest"
}

But when I run

node my-program.js --config ./config.json
Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
  --config   Path to JSON config file
  --test     test message                                           [required]

Missing required argument: test

It's seems it's not getting the test argument from the config.

"yargs": "^17.2.1"

@jly36963
Copy link
Contributor

jly36963 commented Oct 10, 2021

I believe this is because of the difference between cjs and esm. The config function uses require to import the json, and require is specific to cjs. Based on your import statements, I'm assuming you're using esm.

I can confirm that it doesn't work properly:

import yargs from 'yargs'

yargs()
  .command(
    "cmd1",
    "cmd1 desc",
    (yargs) =>
      yargs.option("opt1", {
        describe: "opt1 description",
        type: "string",
        demandOption: true,
      })
      .config(),
    (argv) => {
      console.log({ argv });
    }
  )
  .parse("cmd1 --config ./config.json");

Here's my workaround fix:

import yargs from 'yargs'
import { createRequire } from "module";
const require = createRequire(import.meta.url);

yargs()
  .command(
    "cmd1",
    "cmd1 desc",
    (yargs) =>
      yargs.option("opt1", {
        describe: "opt1 description",
        type: "string",
        demandOption: true,
      })
      .config("settings", require), 
    (argv) => {
      console.log({ argv });
    }
  )
  .parse("cmd1 --settings ./config.json")

You can override the default parsing function by passing a callback as the second argument to config. In this case, I used createRequire (docs) and used require as the custom parsing function.

@jly36963
Copy link
Contributor

I'll work on a fix for this, so that the config method behaves as expected without this workaround.

@bcoe
Copy link
Member

bcoe commented Oct 13, 2021

@jly36963 thanks for digging into this.

@shadowspawn
Copy link
Member

Closing this as fixed by #416

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants