I'm suggesting a new best practice under the code style practices: Avoid backward relative path
Context: it's common to see developers referring code in far-away folders - require('../../othercomponents/file.js')
Problem: encapsulation code smell + maintainability issue
What do you think?
Well the question there would be how to solve this issue. Maybe enabling relative path resolve from the project root, for example not writing require('../../somecomponent/component.js') but instead writing require('somecomponent/component.js'), might help.
Good point, I don't think that there is an agreed best practice for the technique, rather it should just be avoided:
Does it make sense?
Let's say I've got a folder called models and there's a lot of them, so I partition that folder into models/User for user-related models and models/Product for product-related models. There are, of course, relationships between the two, so I'd have to require('../User/Cart') to get at it from the Product model. If I don't have relative requires setup, and many projects don't, this creates a pretty terrible situation where I'm moving files closer together so I can require them while adhering to an ill-advised best practice.
You can't tell people it's best practice to break up a large application into a bunch of NPM packages.
You can't tell people that they have to refactor within modules to not have backwards relative paths.
A better best practice might be "setup relative requires" that encourages you to avoid any require that starts with a directory symbol (., .., /). But even then, there's not really much fungible benefit to having require('foo/bar') over require('../../foo/bar') other than somewhat less visual noise, and there's not really a well accepted best practice for relative requires, is there?
I guess splitting up the application into node/npm modules might be the best way to avoid the backwards relative paths, really. But apart from that I do think that require('foo/bar') would look better than require('../../foo/bar') personally, simply because it's cleaner. There are ways to set up these relative requires from a root directory but this involves a somewhat hacked-together logic. I think the best situation might be either split the application into components that don't need the relative imports, split the application into (npm) modules, or accept the relative imports with directory separators.
@EdwardDrapkin it's not about the visual noise rather what it reveals.
I would split into two cases:
Would love to hear your thoughts
Hello there! 👋
This issue has gone silent. Eerily silent. ⏳
We currently close issues after 100 days of inactivity. It has been 90 days since the last update here.
If needed, you can keep it open by replying here.
Thanks for being a part of the Node.js Best Practices community! 💚
Just sharing all the possible ways to avoid backward relative require: https://gist.github.com/branneman/8048520
Most helpful comment
Good point, I don't think that there is an agreed best practice for the technique, rather it should just be avoided:
Does it make sense?