-
Notifications
You must be signed in to change notification settings - Fork 231
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
Stream is not defined in inherits
call.
#237
Comments
Are you using browserify or webpack? I am not familiar with the Ionic stack. |
@mcollina It uses rollup. UPD: default config |
don't use readable stream with rollup, it doesn't work because rollup doesn't support all of commonjs modules, use the node builtins package and just require streams |
This problem also exists with webpack. You can experience it with pouch-websocket-sync-example v0.1.0. A way around it is to import "stream-browserify" before any other code attempts to import "stream". |
ok @bushev I wrote a plugin for rollup that fixes this including an artisanal hand port of readable-streams called rollup-plugin-node-builtins that should fix your stuff @ericgundrum I'm assuming your talking about webpack 2 in which case you probably want to open an issue there about compatibility with commonjs modules (fun fact I didn't invent this trick, others used it before me) and in the mean time if you can't exclude streams from the bundle like in react-native you should just require('streams') somewhere. |
@calvinmetcalf, yes, this is in webpack 2. I worked around the problem by importing 'stream-browserify' before anything that requires streams. I added comments to their old issue webpack#272. Although it was closed some time ago, it still gets comments. webpack now has a plugin that will detect dependency cycles during compilation. That can help locate the cycle, but you've got to know you have one before you would include the plugin. I was wondering if this could be fixed in In the webpack case, the returned object is totally empty. If I recall their code correctly, they begin with an empty object. Then, while initializing it, the second Because of this failure, the object never gets initialized and webpack reports an error trying to access properties of it. Unfortunately the error message does not indicate which module is having the problem. Certainly webpack could improve on this and benefit other situations. But I think readable-streams could fix this, too -- at least for any loader that returns an object missing the apis it needs. |
ok i think this is a slightly different issue, we intentionally DON'T require streams for this reason and because it is a large package that doesn't add anything, unfortunately there are people who do in other words there shouldn't be a real circular dependency what it sounds like the issue is is that webpack2 isn't actually throwing a run time error when it can't load the package but instead returning an empty object? |
@calvinmetcalf wrote:
That is what I saw when tracing the code runtime in pouch-websocket-sync-example. But I don't know CommonJS well enough to say the webpack2 behavior is wrong. In the websocket app, if I recall correctly, the first encounter is when 'pouch-remote-stream/stream' calls As webpack2 is processing its first encounter with Calvin, I appreciate your having another look at this. If there is anything I can do to help, please ask. |
first off is it undefined or an empty object, the error you posted seams to suggest undefined by the time stream-browserify requires readable stream it's already exported a function, so what webpack2 should have at this point is that function and everything should be fine, but since it apparently is undefined (like in es6 modules) you're getting that error (as opposed to CJS modules if there was a true circular dependency would have an empty object literal which has a prototype so wouldn't give that error) |
Maybe some of my information is incorrect or incomplete. I am retracing it now. This is what I find...
Line 5 is
And Looking up the stack, this call comes from The rest of the stack is just a straight dependency chain -- no cycle. But all this is a symptom, not where the problem originates. I think I need to go look at what happens when 'readable-stream' loads. I'll add more here when I get that. |
ok I was forgetting that superCtor.prototype was what the error was about not superCtor, but that doesn't change that in CJS that would be a function which would have a prototype and that would be the issue, can you post the code that generates this, like all the webpack config and stuff so I can investigate more ? |
The quickest way for you to get into this is to run pouch-websocket-sync-example. I'm pretty sure all you need to do is clone it, |
I set a breakpoint at Then, following the creation of 'readable-stream/transform', |
here's something even weirder, I set a breakpoint here and exports.Transform was defined, then set one here After changing the code to be var stream = require('stream');
var TransformStream = stream.Transform; and stream.Transform was NOT defined there that being said just changing the line to var TransformStream = require('readable-stream').Transform; fixed it |
even more bizzarly transform is being executed due to a different require so it should be defined |
wait but since its a module exports its not getting redefined, but it's just bizzare that it's not being executed at the correct spot |
There seems to be something not right about how Stream.Transform is being created. I remember being in that code when I traced through this problem two weeks ago. I just haven't yet honed in on it. I think it goes bad when, as readable-stream is being instantiated, it creates its transform property. But duplex is in the mix, too. |
I think I see what's happening now. The first require of stream goes something like this.. 'websocket-stream/stream.js' -> 'through2' -> 'readable-stream/transform' -> 'readable-stream/lib/_stream_transform.js' -> './_stream_duplex' -> './_stream_readable' -> 'stream-browserify/index.js' -> 'readable-stream/readable.js' -> './lib/_stream_transform.js' Then 'stream-browserify/index.js' calls 'writable.js' and ''duplex.js'. All looks good so far, until Note that 'readable-stream/transform.js' still is on the stack. Its formation has lead to this call to 'stream-browserify'. At this point, when webpack resolves this second require it returns the unfinished (empty) object it created with the first call to 'readable-stream/transform.js'. As far as webpack is concerned this object is valid and it does a quick return of the empty This test is right at the top of All this unwinds without complaint, but that empty object sits in the webpack cache. Anything that tries to use 'readable-stream/transform.js' will hit it. |
this is the bug in webpack, since it intentionally situates the require call AFTER exporting the function it shouldn't be a problem because dependencies are resolved when they are reached in code |
this is definitly a bug in webpack because I was able to build it with browserify, I'll post a sumery there |
I think this is a good issue for the next wg meeting, e.g. how to improve the consumption of this module in bundlers. |
strictly speaking this module works fine, it's the browserify stream that causes this error but only when imported after through2 and only in webpack |
@calvinmetcalf, thanks for all your work on this. The problem is set up in through2 by its |
@ericgundrum yeah I just realized the same thing and updated the repo |
Import 'pouch-websocket-sync' leads to requiring 'stream'. In the browser, 'stream' is supplied by 'node-libs-browser' requiring 'stream-browserify'. Unfortunately, 'stream-browserify' ultimately requires 'stream' again. (This is to inherit from an exiting definition when one is present.) When webpack encounters this circle, it eventually returns an empty object, supplying an unfulfilled dependency. This is how CommonJS is supposed to work when encountering a cycle. (Node would behave the same if 'stream' were not built in.) Importing 'stream-browserify' early, before any module requires 'stream', creates the base modules before they can get caught in a dependency cycle. This way those module are present when other modules try to import 'stream'. More details of this cycle problem are recorded in [readable-stream issue 237] (nodejs/readable-stream#237) and [webpack issue 4378](webpack/webpack#4378). The behavior of webpack in this scenario appears to be as expected.
we have now removed all of the too clever stream hacks in the browser version of readable-stream so this shouldn't be an issue anymore |
Import 'pouch-websocket-sync' leads to requiring 'stream'. In the browser, 'stream' is supplied by 'node-libs-browser' requiring 'stream-browserify'. Unfortunately, 'stream-browserify' ultimately requires 'stream' again. (Presumably this is to inherit from a built-in definition when the runtime environment provides one.) When webpack encounters this circle, it eventually returns an empty object, supplying an incomplete dependency. This appears to be how CommonJS is supposed to work. Here the circle is avoided by importing 'stream-browserify' before 'node-libs-browser' closes the circle on 'stream'. Importing 'stream-browserify' early effectively imports it with a smaller context where 'stream' is undefined. The 'readable-stream' code is written to handle that case, knowing that 'stream-browserify' actually is providing that definition in some cases. This problem seems similar to one reported as [readable-stream issue 237] (nodejs/readable-stream#237). That was closed, indicating user error. [webpack issue 272](webpack/webpack#272) has info from many others encountering problems with circular dependencies in webpack. [webpack issue 631](webpack/webpack#631) contains a higher level discussion of how the webpack behavior causes hard to identify runtime failures. It also indicates that webpack behaves the same as node, with many folks calling for some sort of warning. The issue was closed with reference to the [circular-dependency-plugin] (https://github.com/aackerman/circular-dependency-plugin). The stack overflow article [circular imports with webpack returning empty object] (http://stackoverflow.com/questions/30378226/circular-imports-with-webpack-returning-empty-object) indicates that this behavior is part of CommonJS. None of the webpack references mention 'stream' as a source of the circular dependency. In fact, they imply that this is not an unusual problem.
@calvinmetcalf, thanks for your follow thru. I no longer see the issue as far as I can tell with my limited testing using readable-stream v2.2.9. |
well, this error still happens to me and is not resolved @calvinmetcalf to reproduce, just |
Is this specific to rollup? |
yes, rollup ... but hard to say, could be related, i dunno know
|
As far as I know, it works ok browserify and webpack. So, it should be a rollupjs problem. |
how can i switch to version 2.2.x? |
rollup is NOT like browserify and webpack so readable stream probably won't work due to its' circular dependencies, that's why I wrote rollup-plugin-node-builtins which includes and ES6ified version of streams that should work. |
yeah i am using that plugin already. so what now? |
see my message in the other issue (calvinmetcalf/rollup-plugin-node-builtins#31), it looks like you are not using it |
I don't think this is resolved for Rollup. |
@ericgundrum, How to solve the Issue. I am using Webpack 2. I am getting the same Error with React-native. can I know the workaround if any? |
@yarra2296, I think the commit comment on my pouch-websocket-sync-example describes it best. The simple answer is to import 'stream-browserify' as early as possible. But note from earlier in this thread that Calvin fixed the problem in 'stream-browserify'. If you still see a problem, maybe you can investigate why and file a new issue as appropriate. |
Hello everyone!
I got the following error on my application bootstrap (Ionic 2 RC):
It seems the second argument
superCtor
that actually aStream
is undefined.The text was updated successfully, but these errors were encountered: