Starting a new project
modules
or features
directories, which is a step in the right direction:Introducing the NX build system
generate workspace
, which leverages the Nx CLI to create a new workspace. Through a series of prompts, we can tailor the workspace to our needs, setting up all the necessary tools, libraries, and dependencies.The project structure
apps
directory as simple as possible, with most of the business logic and templates managed within the feature libraries. Features only expose the essential components to the app, keeping their internal details hidden and maintaining a clean, manageable codebase.Overview
component, we often place its related and internal Grid
component in the same directory, like this:Overview
component is exposed to the outside, while internal components like Grid
stay “hidden.” This keeps the internal structure of your components contained and ensures that only what’s necessary is exposed to the rest of the application. Whether you choose to use index files in this setup is entirely up to you.Input
component in the core library might include several internal components like a label, input field, and error message. Within the feature of the application, you would only interact with the exposed Input
component, keeping its internal structure abstracted.You would use components from the core library:
This approach helps maintain design consistency and lets the core library manage all UI abstractions. As a result, the JSX templates within the features remain cleaner, which significantly improves readability and maintainability.
We know that in theory, this all sounds great, but in practice, you’ll often start by building components within your feature libraries because it’s the quickest way to see results. While this approach is perfectly fine and often necessary, it’s a good habit to ask yourself whether each component is highly specific to that feature or if it has the potential to be reused elsewhere in the application or even in other applications.
Taking a moment to reflect on this can help you manage your components more effectively and keep your codebase organized. Plus, it can save you time in the long run by avoiding redundant work. As you develop, try to keep an eye out for components that might benefit from being part of the core library or shared across different features.
Conclusion
Whether you choose to use Nx or not, the principles outlined in this guide can help you structure your Next.js or React projects more effectively. By organizing your codebase into clear modules and libraries, you’ll create a scalable and maintainable architecture that can grow with your project. While Nx provides a robust foundation and powerful tools to streamline this process, similar results can be achieved with other build systems or custom scripts.
We hope you found this guide helpful! If you have any questions or suggestions, don’t hesitate to reach out. We’d love to hear about how you structure your projects and what strategies work best for you.
Subscribe to our RSS feed
Talk to the author
Contact Erwin
Architect