Pros:
- Main logic file can contain a variety of different functions/processes that don't have to pertain to the function that's being run.
- Function can easily be called from other scripts, but also directly from the command line.
Cons:
- Requires extra file.
export function doThing() {
console.log("I did a thing");
}
import { thing } from './util.js';
thing();
$ node thing.js
Pros:
- File is essentially the function. Self-contained execution scope is minimal and simple for command line usage.
Cons:
- Requires dynamic import if you really want to call it from another module rather than the command line.
- Can't reasonably ingest arguments or return values.
console.log("I did a thing");
$ node thing.js
Pros:
- No "dangling" code. If the file contains a lot of complicated logic, then the "init"/"constructor" initializer function identifies an entry point to the logic.
- Provides an additional scope to variables that may need to be private within the file.
Cons:
- Requires dynamic import if you really want to call it from another module rather than the command line.
- Can't reasonably ingest arguments or return values.
- Might introduce an unnecessary scope.
function init() {
console.log("I did a thing");
}
init();
$ node thing.js