Disclaimer: I’m a Node.js newbie.
There’s a number of class-based languages in which you can/must use namespaces to organize your code, for example: Java, PHP, ActionScript 3… For a number of those languages, if you choose/have to use namespaces, there’s generally a set of common practices and conventions that govern project organization then:
- Classes form the basic code units, and responsibilities are spread across multiple classes.
- The class file hierarchy reside in a single top-level directory (most of the time:
src/
orlib/
). - Each source file contains a single class definition and nothing else.
- Each class resides at a specific level of a namespace (or package) hierarchy, which mirrors the filesystem; for example:
- in Java: class
com.badlogic.gdx.Application
would be found in thesrc/com/badlogic/gdx/Application.java
file - in PHP (with PSR-0): class
Symfony\Component\HttpKernel\Kernel
would be found in thesrc/Symfony/Component/HttpKernel/Kernel.php
file
- in Java: class
- Foreign class symbols can be imported into the current scope via a specific statement:
- in Java:
import com.badlogic.gdx.Application;
- in PHP:
use Symfony\Component\HttpKernel\Kernel;
- in Java:
I’m used to this type of project organization, but I do realize that it’s specific to class/namespace-based languages and that it might not match JavaScript/Node.js’ usual idioms. If I understand the concept of Node.js modules correctly, it’s 1 source file = 1 module, but from what I’ve seen in a lot of NPM packages, a module usually export more than one symbol, and more often than not those exports are functions and not classes/constructors, so it’s pretty different from the conventions described above.
So, I have the following questions:
- In JavaScript/Node.js, is it relevant at all to think about distribution of responsibilities in terms of «classes only» (using either the traditional constructor + prototype composition method or the new
class
shorthand)? - Is the type of project organization described above possible at all in the context of a Node.js project?