MattiasBuelens / web-streams-polyfill

Web Streams, based on the WHATWG spec reference implementation
MIT License
289 stars 29 forks source link

ERR_UNSUPPORTED_DIR_IMPORT #87

Closed loynoir closed 2 years ago

loynoir commented 3 years ago

Reproduce

$ yarn add web-streams-polyfill
// test.mjs
import { ReadableStream as PonyReadableStream } from  'web-streams-polyfill/ponyfill'
console.log(PonyReadableStream)

Expect

No error

Actual

$ node test.mjs
internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'XXX' is not supported resolving ES modules imported from XXX/test.mjs
Did you mean to import web-streams-polyfill/dist/ponyfill.js
  code: 'ERR_UNSUPPORTED_DIR_IMPORT',
  url: 'file:///XXX/web-streams-polyfill/ponyfill'
}

Else

import { ReadableStream as PonyReadableStream } from  'web-streams-polyfill/dist/ponyfill.js'
console.log(PonyReadableStream)
$ node test.mjs 
[Function: ReadableStream]
MattiasBuelens commented 3 years ago

The polyfill does not yet fully support native ESM in Node. I'm currently working on this for v4.0 in #83, which should land soon(ish).

For now, you can work around this by importing the .mjs file directly (as suggested by the error message):

import { ReadableStream as PonyReadableStream } from  'web-streams-polyfill/dist/ponyfill.mjs'
MattiasBuelens commented 2 years ago

This is implemented in #83. Give it a spin with the latest beta release: v4.0.0-beta.3. 😉

Note that the polyfill's exports have changed in 4.0, see MIGRATING for more information. Your example would now look like this:

// test.mjs
import { ReadableStream as PonyReadableStream } from 'web-streams-polyfill'
console.log(PonyReadableStream)