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

fileTypeStream broken import #648

Closed
tvthatsme opened this issue Jul 24, 2024 · 11 comments
Closed

fileTypeStream broken import #648

tvthatsme opened this issue Jul 24, 2024 · 11 comments

Comments

@tvthatsme
Copy link

We are updating from 19.0.0 to 19.0.3 and facing a seeming breaking change.

Had been using:

import { fileTypeStream } from "file-type/core";

But see that fileTypeStream has now been moved to the index file. So I expected that just changing the path would work.

import { fileTypeStream } from "file-type";

However, the module exports don't provide a path to import this function. The node imports work fine but the default is mapping to ./core.js so it's breaking the TypeScript compiler.

Is this something we could get reverted/adjusted to so the importing of fileTypeStream works as expected?

@sindresorhus
Copy link
Owner

But see that fileTypeStream has now been moved to the index file. So I expected that just changing the path would work.

That is indeed an unintended breaking change. I think we all had forgotten about the /core sub-export.

The node imports work fine but the default is mapping to ./core.js so it's breaking the TypeScript compiler.

It sounds like your bundler/compiler config is incorrect. You should be getting the node export, not default.


// @Borewit

@tvthatsme
Copy link
Author

Thanks for the response! It was all working fine (including bundle/compiler config) prior to the update so I think it's probably just the breaking change that is the cause.

@Borewit
Copy link
Collaborator

Borewit commented Jul 26, 2024

I am sorry if I have broken something.
I am currently traveling and cannot quickly fix something.

If you consider getting rid of it, I do not mind. Not sure if the /core sub export adds much value. I had trouble compiling Angular code using such sub export.

@Borewit
Copy link
Collaborator

Borewit commented Jul 26, 2024

Not a mistake at all.

In PR #635, introducing web stream support, the fileTypeStream has been moved to Node.js specific entry point as it is takes Node.js Readable stream as an argument, see discussion here: #635 (comment)

We should add Web Stream flavour of fileTypeStream to the default entry point (which is the same as the /core sub export). I propose to keep this issue open, as request to add that function.

Update: Add web stream support for fileTypeStream(): #649

@tvthatsme
Copy link
Author

tvthatsme commented Jul 26, 2024

Thanks for the info. I guess the problem I am seeing is that with that change (introduced in 19.1.0) TypeScript doesn't understand where to look for the type information anymore. It works when running the app but not for the tooling.

I was able to fix it on my side by adding a type definition file but this seems less than ideal. Maybe I am missing something?

import { Readable } from "node:stream";
import { FileTypeResult } from "file-type";

declare module "file-type" {
	export type ReadableStreamWithFileType = Readable & {
		readonly fileType?: FileTypeResult;
	};

	export async function fileTypeStream(readable: Readable): Promise<ReadableStreamWithFileType>;
}

@sindresorhus
Copy link
Owner

@tvthatsme Can you try this branch? 7548a62

npm install 'sindresorhus/file-type#7548a6222e21c2000265cc4e5052ca595f1b3b6b'

@tvthatsme
Copy link
Author

Hey @sindresorhus thanks for looking at it a bit more! I tried your branch, together with deleting the type definition file I had created, but got the same error when running typecheck:

error TS2724: '"file-type"' has no exported member named 'fileTypeStream'. Did you mean 'fileTypeFromStream'?

3 import { fileTypeStream } from "file-type";

I think there is something going on where typechecking is looking at the default or core type definitions, but the app is really just using the node methods.

At this point, I have the type definitions working and am not blocked. So I'm fine to close this issue if it's a "just me" situation. I appreciate the support in looking at this issue!

@Th3Vladimir
Copy link

Same issue here

@Borewit
Copy link
Collaborator

Borewit commented Aug 14, 2024

Same issue here

That is very weird, as since v19.4.0, both entry points (Node & default) export fileTypeStream().

@minicacristiancolleva
Copy link

same issue

@Borewit
Copy link
Collaborator

Borewit commented Aug 26, 2024

The export of fileTypeStream() has been re-added in v19.4.0, therefor I conciser this issue as resolved.

@Borewit Borewit closed this as completed Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants