DEV Community

Daniel Sellers
Daniel Sellers

Posted on • Originally published at designfrontier.net on

Modules not Globals

Often newer JS developers ask when they should turn something into a standalone module. When should that function turn into its own file? There are lots of things we could talk about with componentized architecture. But there is a single thing that should be a massive flashing sign that you need a module.

That thing is global variables. Anytime you touch window. or global. or some other shared variable scope.

Next time you are trying to share things through a global scope... grab a module that you can require into many modules. Usually these wrappers are incredibly light, and they will make your code much cleaner.

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more