question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ts-jest says `The requested module 'yargs/helpers' does not provide an export named 'hideBin'`

See original GitHub issue

Hi, I have a question.

I’m going to use yargs in the project have ts-jest.

ts-jest said The requested module 'yargs/helpers' does not provide an export named 'hideBin' but I know yargs/helper have export function hideBin

🐚 log

yarn run v1.22.17
$ node --experimental-vm-modules node_modules/jest/bin/jest.js
(node:22533) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 FAIL  src/main.spec.ts
  ● Test suite failed to run

    SyntaxError: The requested module 'yargs/helpers' does not provide an export named 'hideBin'

      at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:779:5)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:401:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.485 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

This script that imported in main.spec.ts invoked this error.

setup.ts

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import path from "path";
import dotenv from "dotenv";
const __dirname = path.dirname(new URL(import.meta.url).pathname);

dotenv.config({ path: path.resolve(__dirname, "../.env") });
const args = yargs(hideBin(process.argv)).parse();

export default args;

I solved this problem not importing hideBin.

setup.ts (resolved problem)

import yargs from "yargs";
import path from "path";
import dotenv from "dotenv";
const __dirname = path.dirname(new URL(import.meta.url).pathname);

dotenv.config({ path: path.resolve(__dirname, "../.env") });
const args = yargs(process.argv.slice(2)).parse();

export default args;

It’s mysterious. Someone knows why this problem happen?

I’m using this jest.config.js

module.exports = {
  // [...]
  preset: "ts-jest/presets/default-esm", // or other ESM presets
  globals: {
    "ts-jest": {
      useESM: true,
    },
  },
  moduleNameMapper: {
    "^(\\.{1,2}/.*)\\.js$": "$1",
  },
};

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bcoecommented, Jan 24, 2022

@AndyClausen I believe the ESM version of yargs drops the magic .argv accessor, try the following:

import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'

yargs(hideBin(process.argv))
  .command('curl <url>', 'fetch the contents of the URL', () => {}, (argv) => {
    console.info(argv)
  })
  .demandCommand(1)
  .parse()

.parse() is equivalent to .argv but less magic.

0reactions
praveendvdcommented, Feb 5, 2022

@praveendvd could you open an issue on ts-jest as well, perhaps they have some direction? This seems like a bug with how they’re handling ESM vs., CJS requires.

https://github.com/facebook/jest/issues/12284

Already did it thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest, ES6 modules "does not provide export named"
The problem I have is that my app is built with ES6 modules and Jest ES6 modules is experimental and it seems that...
Read more >
the requested module 'path' does not provide an export ...
ts -jest said The requested module 'yargs/helpers' does not provide an export named 'hideBin' but I know yargs/helper have export function hideBin.
Read more >
The requested module does not provide an export named in JS
The "Ucaught SyntaxError: The requested module does not provide an export named" occurs when mixing up default and named ES6 module imports and...
Read more >
Additional documentation - yargs - JS.ORG
Calling .parse() with no arguments is equivalent to calling .argv : ... Note: Yargs exposes the helper hideBin , which handles the process.argv.slice...
Read more >
Настройка Jest
To read TypeScript configuration files Jest requires ts-node . Make sure it is installed in ... Node.js core modules, like fs , are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found