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

Can't import {Buffer} from 'buffer', same with EventEmitter/events #3723

Open
josephsavona opened this issue Apr 13, 2017 · 2 comments
Open
Labels
Library definitions Issues or pull requests about core library definitions

Comments

@josephsavona
Copy link
Contributor

Flow (tested with 43 and 42) rejects the following line, claiming that buffer does not have an export named Buffer:

import {Buffer} from 'buffer';

It allows import Buffer from 'buffer', except that this doesn't work: 'buffer' doesn't export a default value, so that throws when compiling ES6 imports to require statements e.g. with Babel.

The same is true for import {EventEmitter} from 'events', which works but is rejected by Flow.

The use case I'm trying to solve is explicitly Buffer/EventEmitter so that I can produce node builds (using the actual node modules) and browser builds (rewriting those imports to 'buffer/' and 'events/') that require browser polyfills for them.

@vkurchatkin vkurchatkin added the Library definitions Issues or pull requests about core library definitions label Apr 13, 2017
facebook-github-bot pushed a commit that referenced this issue May 1, 2018
Summary:
Previously discussed or implemented in #5153, #3495, #2938, #3723, #3724 and maybe elsewhere.

This PR refines previous efforts by adding:

1. A correct, unambiguous type for `require("buffer").Buffer`:
```jsx
declare var Buffer: typeof global.Buffer;
```

2. A test.

To be clear: Per the Node [docs](https://nodejs.org/api/buffer.html), explicitly importing `Buffer` is rarely needed, given its availability in the global scope, but correct. It happens to be a viable option in environments where "magic" support for Node globals is either undesirable or broken (the latter is [currently](webpack/webpack#7032) the case for ES modules in Webpack).
Closes #6179

Reviewed By: gabelevi

Differential Revision: D7755694

Pulled By: fishythefish

fbshipit-source-id: d6473d4291512624c7bd7b59dc18ae5b90383abb
@gre
Copy link
Contributor

gre commented Oct 20, 2018

this is still not fixed?

despite the PR that was supposed to support Buffer, I'm having it uncovered:

capture d ecran 2018-10-20 a 10 31 11

tried both require() and import syntaxes

@Brianzchen
Copy link
Contributor

@gre this was fixed to a degree. There's no flow error anymore because Buffer is defined in buffer but it's just not typed properly so it defaults to any.

I think in general this issue is resolved. Though looking at the definition, I don't know why it was defined as Buffer: typeof global.Buffer instead of Buffer: Buffer which may make it more useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Library definitions Issues or pull requests about core library definitions
Projects
None yet
Development

No branches or pull requests

4 participants