sindresorhus / meow

šŸˆ CLI app helper
MIT License
3.53k stars 150 forks source link

Improve importMeta message -`meow - The 'importMeta' option is required. Its value must be 'import.meta' at meow` #222

Closed dilraj-vidyard closed 1 year ago

dilraj-vidyard commented 1 year ago

I apologize if this is clear for most people, but I've dealt with a lot of headaches with node --experimental-modules.

My Experience

When I imported meow, with:

import meow from 'meow';

And did const cli = meow();,

This is what I saw:

(node:23486) ExperimentalWarning: Importing JSON modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
file:///Users/dsingh/Documents/node-cli/dilraj/node_modules/meow/index.js:110
    if (!(options.importMeta && options.importMeta.url)) {
                  ^

TypeError: Cannot read properties of undefined (reading 'importMeta')
    at meow (file:///Users/dsingh/Documents/node-cli/dilraj/node_modules/meow/index.js:110:16)

I was convinced that it was something to do with the ES module stuff and it took me down the wrong path for hours.

The Solution

It wasn't until I went to the docs and searched specifically for the importMeta; (šŸ˜®ā€šŸ’Ø definitely should have done that soon šŸ¤¦)

const cli = meow(); // āŒ TypeError: Cannot read properties of undefined (reading 'importMeta')

const cli = meow({ importMeta: import.meta }); // āœ… This fixed it

Proposal

  1. BOOST the error message on Google Thus, I decided to open an issue in case anyone else searches for the same issue.

  2. Maybe provider a clearer message?

I don't know enough about meow to offer a better message.

so1ve commented 1 year ago

:-(

dilraj-vidyard commented 1 year ago

I know :(((( Forgive me

LitoMore commented 1 year ago

I believe the error message is clear enough. You should read our documentation and examples before use.

dilraj-vidyard commented 1 year ago

nice

u must be fun at parties

LitoMore commented 1 year ago

lol. Do you mean we didn't explain in the documentation why we mark this parameter as required?

Hmmm, maybe you are right.

Feel free to reopen it if you would like to discuss further.

chuyaguo2014 commented 1 year ago

Coming here to thank you OP for posting this!

I had the same issue. šŸ˜… It wasn't immediately clear to me in the example from the documentation that the importMeta was required (I thought it was part of the set up like the rainbow boolean option so it was only relevant for that particular šŸ¦„ example).

I also misunderstood the error message. I thought

"TypeError: The `importMeta` option is required. Its value must be `import.meta`." 

meant I had to do something like:

{  importMeta: 'some-magic-meta-data-for-my-module-maybe-the-file-path-or-something' }

or maybe something more literal like:

{ importMeta: `import.meta` }

I believe I misread the documentation because the first example is a little long and the required options got buried by the rest of the example. Maybe something like this would be more obvious?

#!/usr/bin/env node
import meow from 'meow';
import foo from './lib/index.js';

const cli = meow(`text to display for the --help option`, 
  {
    importMeta: import.meta, // required 
    flags: {
    rainbow: {
        type: 'boolean',
        shortFlag: 'r'
    }
  }
});
/*
{
    input: ['unicorns'],
    flags: {rainbow: true},
    ...
}
*/

foo(cli.input.at(0), cli.flags);

and then later on give a longer example where your users can describe the usage in more details? šŸ™‚ šŸ™šŸ¼

tommy-mitchell commented 1 year ago

@chuyaguo2014 you should open a new issue suggesting more documentation. I like adding the // Required comment to the usage example.