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

fix: import yargs/yargs in esm projects #2151

Merged
merged 4 commits into from
May 11, 2022
Merged

Conversation

hongaar
Copy link
Contributor

@hongaar hongaar commented Mar 23, 2022

When using import yargs from "yargs/yargs" in an ESM project ("type": "module" in package.json), Node.js will throw this error:

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for /home/runner/work/bandersnatch/bandersnatch/tests/smoke/esm/node_modules/yargs/yargs
    at new NodeError (internal/errors.js:322:7)
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
    at Loader.getFormat (internal/modules/esm/loader.js:105:[42](https://github.com/hongaar/bandersnatch/runs/5661229728?check_suite_focus=true#step:8:42))
    at Loader.getModuleJob (internal/modules/esm/loader.js:2[43](https://github.com/hongaar/bandersnatch/runs/5661229728?check_suite_focus=true#step:8:43):31)
    at async ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:78:21)
    at async Promise.all (index 3)
    at async link (internal/modules/esm/module_job.js:83:9) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'

To solve this, add a new yargs.mjs file and update the ./yargs export in the package.json.

Confirmed that this works for a simple ESM project where yargs is a transitive dependency: hongaar/bandersnatch#392 (in tests/smoke/esm)

Related to #2014

@bcoe
Copy link
Member

bcoe commented Mar 23, 2022

@hongaar what Node.js versions does this problem arise in? It sounded like this was an error that begins happening in v17, according to #2014.

I believe the yargs.mjs file caused issues on some Node versions, which is how I landed on an yargs with no extension.

@hongaar
Copy link
Contributor Author

hongaar commented Mar 23, 2022

This is already happening with Node.js >= 12 as far as I can see.

The bandersnatch CI fails with this error for all common versions of Node.js (https://github.com/hongaar/bandersnatch/actions/runs/2029079244)

The failing test is running a ESM project with this package.json:

{
    "name": "bandersnatch-smoke-test-esm",
    "type": "module",
    "scripts": {
        "test": "node index.mjs"
    },
    "dependencies": {
        "bandersnatch": "^1.9.2"
    }
}

yargs is loaded through the bandersnatch dependency with:

import createYargs from 'yargs/yargs';

@hongaar
Copy link
Contributor Author

hongaar commented May 11, 2022

@bcoe Is there anything I can do to help this get merged? I could add a simple smoke test (like in the bandersnatch project) to confirm this change is working as intended?

@bcoe bcoe merged commit 95aed1c into yargs:main May 11, 2022
@bcoe
Copy link
Member

bcoe commented May 11, 2022

@hongaar thank you for the patch, hopefully it does the trick for you.

@hongaar hongaar deleted the fix/yargs/yargs branch May 12, 2022 07:37
hongaar added a commit to hongaar/yargs that referenced this pull request May 12, 2022
Due to the `yargs.mjs` file not being published, the fix merged with yargs#2151 is not effective yet. This change should fix that.

```
Cannot find module './node_modules/yargs/yargs.mjs' imported from ./cli.js
```
@hongaar
Copy link
Contributor Author

hongaar commented May 12, 2022

@bcoe thanks for merging this! I forgot to add the new file to the package.json list array and opened a new PR here to fix this: #2185

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

Successfully merging this pull request may close these issues.

2 participants