-
-
Notifications
You must be signed in to change notification settings - Fork 284
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
[ESM Support] Import named 'prompt' not found in module #439
Comments
I notice that if I replace the (by doing this I lost TS intelisense) |
Nice! I was just thinking about this a few days ago. I've used I'm working on the next major and I'll try to make sure it works with bun out of the box. In the meantime, if we figure out the cause of the error and you (or someone else) wants to do a PR, that would be great.
Hmm, I have a hunch about what's causing that. But first, maybe try these approaches and see if they work: import Enquirer from 'enquirer';
const enquirer = new Enquirer();
// either this...
const response = await enquirer.prompt({
type: 'input',
name: 'username',
message: 'What is your username?'
});
// or this
const response = await Enquirer.prompt({
type: 'input',
name: 'username',
message: 'What is your username?'
}); Regarding the issue with using import, my hunch is that it's almost certainly that the I'll definitely look into this more, and I'd love to hear about any progress you make or issues you have using enquirer with bun. thanks for creating the issue! |
TLDR: import Enquirer from "enquirer";
const { prompt } = Enquirer; 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. // prompt.ts
const { prompt } = require("enquirer");
This error happens because bun declares a global variable // tsconfig.json
{
"types": ["bun-types"]
} This can be fixed in 2 different ways:
// prompt.ts
const { prompt: enquirerPrompt } = require("enquirer");
// prompt.ts
const { prompt: enquirerPrompt } = require("enquirer");
export {}; 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: // prompt.ts
import { prompt } from "enquirer"; 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: import Enquirer from "enquirer";
const { prompt } = Enquirer; 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 - module.exports = Enquirer;
+ export default Enquirer;
+ export const prompt = Enquirer.prompt; 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. |
Okay, thanks for the update. Do you have any info on the other bugs, so we can try to resolve them?Sent from my iPhoneOn Sep 19, 2023, at 5:10 AM, Sigurd Schoeler ***@***.***> wrote:
Hi, there are multiple bugs with enquirer, and only one of these is specific to bun.
If the file is treated as a CommonJS module
// prompt.ts
const { prompt } = require("enquirer");
Cannot redeclare block-scoped variable 'prompt'.ts(2451)
types.d.ts(2442, 18): 'prompt' was also declared here.
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:
// tsconfig.json
{
"types": ["bun-types"]
}
This can be fixed in 2 different ways:
Rename as we import prompt to fix the a naming conflict:
// prompt.ts
const { prompt: enquirerPrompt } = require("enquirer");
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:
// prompt.ts
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
Bun works perfectly with enquirer, i have used it in my very small project, and haven't seen any bugs related to bun. All of the errors i have seen are because of incorrect types, and are fixed in open pull requests. Bun uses typescript by default, which is why the typescript errors are incorrectly labeled as bun errors. Many of the bugs i encountered are fixed in this open PR: #307 Moving the existing work done in PR #307 to definitelytyped would not be too difficult. |
@raulfdm Could you try to import enquirer like this? Then it should work correctly with bun.
@jonschlinkert I think this can be closed, as most of the issues are already tracked in other issues. |
@Zikoat, in that way, it works indeed Should the README include a side note for that case? |
Yes it probably should have a note for how to import the package using ESM |
Anyway, if you try to import Form like this:
You will encounter a problem with typescript because Form does not exist in the type 'typeof Enquirer'. |
@jesusvallez , using |
But you are not importing Form. |
Ahh... now I see. Well, debugging the code base this is more than expected. There's a mismatch between the code and what's being actually exported. There's a Lines 10 to 28 in 70bdb0f
While this works in CJS because there's no strict check on what's being declared vs what's being imported, in TS this won't work, unless we define ALL submodules of This lib was written 5 years ago, maybe it would worth a rewrite either in TS or in some way that's explicit what's being exported or not (if in JS, using proper JSDocs annotation) |
@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:
I would suggest to avoid documenting the workaround syntax ( |
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: |
Nice, thanks for the links! Watching those PRs now too :) |
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.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
: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:
Binaries:
The text was updated successfully, but these errors were encountered: