Pavan
Pavan

Reputation: 1015

What's the behavior of webpack imports in ReactJS project?

I've used multiple 3rd party libraries in my ReactJS project like lodash, d3 etc. I just found out that I've not written explicit imports like import d3 from 'd3' or import _ from 'lodash' in all my components (I've imported them in some though). Yet it all works fine and I can also get the d3 object and _ in the browser console. How is it supposed to be? Considering this is okay behavior can I just import the node_modules dependencies for react only once in my App(Root) Component and don't import them at all in all the other child components.

P.S I'm using webpack 1 and I've verified the behavior.

Upvotes: 1

Views: 32

Answers (1)

Davorin Ruševljan
Davorin Ruševljan

Reputation: 4392

Even when it works, it is a bad practice, so my advice would be to play nice and always explicitly import modules you are using.

The reason why it is working is probably because some of those modules declare globals when they are imported, so your components that do not import them still reach global.

Upvotes: 1

Related Questions