bun
bun copied to clipboard
Yargs does not work with Bun.
What version of Bun is running?
0.2.1 (canary)
What platform is your computer?
Darwin 21.6.0 Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:25 PDT 2022; root:xnu-8020.140.41~1/RELEASE_X86_64 x86_64 i386
What steps can reproduce the bug?
- Create a new directory
bun initbun add yargsbun add -d @types/yargs- Use the code from their example
bun index.ts
How often does it reproduce? Is there a required condition?
Every time.
What is the expected behavior?
The program runs without error.
What do you see instead?
TypeError: Invalid regular expression: invalid flags while parsing module "/Users/elirichardson/buntest/node_modules/yargs/yargs"
at /Users/elirichardson/buntest/index.ts:1:22
Additional information
NOTE: In the docs for bun there is one example showing you can import argv from bun.
However, typescript errors saying Module '"bun"' has no exported member 'argv'.
However, after bun index.ts that error is ignored and I get the same error as below.
Here is what my VS Code says:

Confirmed on Ubuntu in virtual machine:
Linux 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64
I've gotten it working by
- calling
.parse(process.argv)instead of.argvat the end of the chain yargs/build/lib/utils/set-blocking.js
export default function setBlocking(blocking) {
if (typeof process === 'undefined')
return;
[process.stdout, process.stderr].forEach(_stream => {
++ if (_stream) {
const stream = _stream;
if (stream._handle &&
stream.isTTY &&
typeof stream._handle.setBlocking === 'function') {
stream._handle.setBlocking(blocking);
}
++ }
});
}
yargs/lib/platform-shims/esm.mjs
-- stdColumns: typeof process.stdout.columns !== 'undefined' ? process.stdout.columns : null
++ stdColumns: process && process.stdout && typeof process.stdout.columns !== 'undefined' ? process.stdout.columns : null
Very hacky, so hesitant to PR, but seems to work
@robobun
var argv = require('yargs/yargs')(process.argv.slice(2)).argv;
if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!');
} else {
console.log('Retreat from the xupptumblers!');
}
@Electroid here you go!
Retreat from the xupptumblers!
Code
var argv = require('yargs/yargs')(process.argv.slice(2)).argv;
if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!');
} else {
console.log('Retreat from the xupptumblers!');
}
Ran using the latest build of Bun, an all-in-one JavaScript runtime.
yargs should work in Bun v0.3.0, if you're still having issues please feel free to open another issue.
@Electroid here you go!
1 | yargs;
^
ReferenceError: Can't find variable: yargs
at /tmp/bun-9zJHg9/index.tsx:1:0
Code
yargs
Ran using the latest build of Bun, an all-in-one JavaScript runtime.