enquirer: [Bun] Import named 'prompt' not found in module
Hey peps 👋 .
I know it’s too early, but I was trying to run enquirer
with Bun, and I got a problem not related to the environment but maybe to the way the bundle is being shipped.
SyntaxError: Import named 'prompt' not found in module '/home/runner/enquirer-test/node_modules/enquirer/index.js'.
Reproduction
-
create a folder:
take enquirer-with-bun
; -
start an empty project using bun:
bun init -y
; -
add
enquirer
:bun add --exact enquirer
; -
paste the following code in the
index.ts
:import { prompt } from "enquirer"; const response = await prompt({ type: "input", name: "username", message: "What is your username?", }); console.log(response); // { username: 'jonschlinkert' }
-
run the index file:
bun index.ts
; -
see the error;
You can also see a code demo on this replit: https://replit.com/@raulfdm/enquirer-test?v=1
Environment
System:
- OS: macOS 14.0
- CPU: (10) arm64 Apple M1 Pro
- Memory: 210.27 MB / 32.00 GB
- Shell: 5.9 - /bin/zsh
Binaries:
- Node: 18.15.0
- Yarn: 1.22.0
- npm: 9.7.2
- pnpm: 8.7.5
- Watchman: 2023.05.22.00
- Bun: 1.0.2
About this issue
- Original URL
- State: open
- Created 9 months ago
- Reactions: 1
- Comments: 15 (2 by maintainers)
Ah yes, you’re right about keeping this open, and renaming.
I was talking about these open pull requests, which would fix this issue if they were merged:
@Zikoat which issues do you mean? The only other issues mentioning ESM are these:
Both of which have a lot of extra requests, other than ESM support.
This Bun issue (#439) is the closest that we have to a request only for ESM support.
I would suggest that:
This issue should not be closed
@raulfdm could edit the title of this issue to be ESM support, to support named imports:
I would suggest to avoid documenting the workaround syntax (
import
and then destructuring), because it should be actually fixed inenquirer
.TLDR:
Hi, there are multiple bugs with enquirer, and only one of these is specific to bun, and only happens if my file is a CommonJS module.
This error happens because bun declares a global variable
prompt
which would be overwritten by the require import. This is done at runtime, but we are getting typescript errors too because we add the global bun definitions with this line:This can be fixed in 2 different ways:
However, we don’t get any typescript by importing it like this. I am unsure of why this happens, but it seems to be a more general problem with the typescript types. We can get the typescript hinting by using ESM imports:
However, this gives us the next error:
This error is not specific to bun, as it also happens when running it with node. In fact, the error we get from node is a lot more helpful:
This hints us that enquirer is a CommonJS module, which only supports default exports. We can fix our code by editing it to:
This code works in both node and bun. @jonschlinkert Could you add this to the documentation as long as the fix below isn’t implemented?
To fix this issue, (which is actually a mismatch between the typescript types and the actual exports) we should allow
import { prompt } from "enquirer";
by changing this line:/node_modules/enquirer/index.js
But I don’t have the time to create a proper pull request, test it, check if there are some more exports that would need to be exported to match the TS type definitions, or if this is a backwards incompatible change which would break CommonJS projects that are using this, and would warrant a Major version bump.