Modules
37 bites tagged Modules — interview questions with model answers, and 60-second explainers.
TypeScript's Module Resolution Strategy
TypeScript's `moduleResolution` is the compiler's search plan for finding files behind `import` statements. It's crucial when mixing module types (ESM/CJS) or targeting Node.js vs. the browser. The footgun is assuming the default `node` works for modern ESM.
Python Packages: Grouping Modules with __init__.py
A Python package is a folder of modules treated as one unit. The `__init__.py` file marks the folder as a package and can run setup code. Use it to organize large codebases.
Node.js Circular Dependencies: The Unfinished Export
When module A requires B, and B requires A, Node.js avoids an infinite loop by returning an unfinished version of one module's exports. This happens in complex apps with tightly coupled modules. The code doesn't crash; it fails later with a TypeError.
CommonJS: Node.js's Original Module System
CommonJS treats each file as a private box of code. You share tools using `exports` and import them with `require`. It's the original module system for Node.js, used to organize code into reusable pieces.
Custom Frameworks: Best Practices for Sharing Code
A custom framework is a private toolkit, bundling shared code and resources for use across multiple apps. Use them for common logic in an app suite, but always prefix public symbols to avoid name collisions and static link errors.
Rust Unit Tests: Co-locating Tests with Code
In Rust, unit tests live inside a special `tests` module within the same file as the code they're testing. This lets you test a module in isolation, including its private functions.
Go's `internal` Directory: Private by Convention
Go's `internal` directory creates private packages within your module, making them inaccessible to external projects. Use it for helper logic you don't want to support as a public API.
Rust's Module-to-Filesystem Mapping
Rust's module system maps directly to your file system. A `mod foo;` statement tells the compiler to look for `foo.rs` or `foo/mod.rs`. This is how you organize any multi-file Rust project.
Rust Item Visibility: Private by Default
In Rust, all items are private by default. Think of modules as locked rooms; you need the `pub` keyword to unlock the door. This is crucial for creating a public API or letting modules interact.
Rust Modules: Your Code's File System
Think of Rust modules as a file system for your code, grouping logic and hiding details. You declare them with `mod`, and Rust finds the code in corresponding files. The footgun: items are private by default, so you must use `pub` to expose them.
Rust Crates: Your Unit of Compilation
A crate is the smallest unit of code the Rust compiler handles—either a runnable program (binary) or a shareable library. A package, defined by Cargo.toml, bundles one or more crates. The footgun: a package can have many binaries but only one library.
ESM vs. CJS: Navigating JavaScript's Module Divide
JavaScript has two module systems: modern, static ESM (`import`) and legacy, dynamic CJS (`require`). When publishing a library, you must support both. The footgun is shipping only ESM, as it breaks downstream projects that still use `require()`.
Sass Built-in Modules: Namespaced Power Tools
Think of Sass built-in modules as namespaced toolkits for color, math, and more. You explicitly import what you need, like using `sass:color` for theming. The footgun is relying on old global functions, which ties you to legacy Sass implementations.
Get Modules bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.