Skip to content

Importing iconv-lite in Vite warns "Module "stream" has been externalized for browser compatibility. Cannot access "stream.Transform" in client code" #392

Description

@jeremy-code

Provide a general summary of the bug here

In Vite, when importing iconv-lite, a warning will appear stating stream.Transform can't be accessed, linking to these Vite troubleshooting docs:

...If the module is imported from a third-party library (that's meant to be used in the browser), it's advised to report the issue to the respective library.

It seems that this is the affected code.

iconv-lite/lib/index.js

Lines 163 to 167 in 6cf4f08

// Enable Streaming API automatically if 'stream' module is available and non-empty (the majority of environments).
var streamModule
try {
streamModule = require("stream")
} catch (e) {}

Vite team member bluwy has said (vitejs/vite#9837 (comment))

Looks like the reason deps access them, even if there are browser friendly is because they are doing a "try import and test if exist" flow, which isn't a great technique for browser compatibility. They should not use the nodejs module at all via browsers and exports field.

Steps to Reproduce

Import iconv-lite in Vite while having a Buffer polyfill.

Module "stream" has been externalized for browser compatibility. Cannot access "stream.Transform" in client code. See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser
-compatibility for more details.

StackBlitz

Versions

iconv-lite@0.7.2
vite@8.0.14

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions