4 ms·
For me, a relative path in a require statement signifies that the required code is a part of the application itself. Consider: require('foo'); Versus:
by jt2190 13y ago
For me, a relative path in a require statement signifies that the required code is a part of the application itself.
Consider:
require('foo');
Versus:
require('./foo');
If I find that a module is becoming popular, I may turn it into an npm module by moving it to it's own folder and adding a package.json, then using npm's bundledDependencies property to inform npm that it lives locally.
(edit: Note that I've shifted my perspective on the module when this happens. The shift is from "This code is in a separate module to keep my application organized" to "This code is in a separate module because it has some logical independence from the application.")
If the module needed even more independence, I'd move it to it's own git repo and create an internal mirror of the npm repo so that it can be discovered internally, and unbundle it from my app. (I could see this happening especially with larger teams, or teams that embrace the "module all the things" philosophy.)
And of course, there's always the possibility of moving the module out to the public npm repos and hosting it there.
I've never had require with relative paths of more than two or three "double dots". (Note to self: Look up what those things are called formally.)
- jaredsohn 13y agoThe double dots refer to the parent directory; I'm not sure that there is other associated terminology or if there is an easier way to express what you wrote.
- 2mur 13y agoIf find it's easier to distinguish modules from files by requiring modules like: require('foo'); and application files like: require('./foo.js'); //.js extension
- qubyte 13y agoImagine if you're requiring that local module throughout your codebase. Now suppose that the module has become large enough to be come a directory. All those requires with the .js extension will break! If you leave it off, then no problem. Always avoid adding the .js.