Modules
37 bites tagged Modules — interview questions with model answers, and 60-second explainers.
Circular dependencies in CommonJS modules
When A requires B which requires A, the cache returns A's partial exports; fields defined later are undefined at that moment. deep understanding of CommonJS loading.
Choosing between CommonJS and ES Modules
ESM is the standard with static import/export and top-level await; set type to module; CJS uses require and module.exports and is synchronous. knowing the two module systems and their config.
Resolving core vs relative module specifiers
'fs' is a built-in core module loaded by name with top priority; './my-file.js' is a relative path resolved from the current file. knowledge of module resolution rules.
Strategy for large multi-team IaC projects
Versioned reusable modules, state split per environment and component, promotion of identical code via variables, and externalized secrets. scaling IaC organizationally. one giant shared state or secrets in code.
Type a UMD library for script tags and CommonJS/ESM
Tests TypeScript UMD declarations for dual global and module usage. Use export as namespace MyLib for the global, export = MyLib for require, and declare the API inside a namespace. Red flag: default exports or declare global instead of export as namespace.
How does export = differ from export default in TypeScript declarations?
It tests CommonJS to ES module interop in TypeScript. export = maps to the entire module.exports object and must be imported with import = require() or ES interop, while export default creates a named binding. A red flag is claiming they are interchangeable.
How do you configure a dual ESM/CommonJS package?
Use exports.require for .cjs and exports.import for .mjs, keep type unset or explicit, and compile separate artifacts. Node.js conditional exports and the dual-package hazard.
What problem does esModuleInterop solve for CommonJS imports?
Tests CommonJS-to-ESM interop. A strong answer covers: default imports for CommonJS modules; runtime helpers checking __esModule and wrapping exports; and implicit allowSyntheticDefaultImports. Red flag: claiming it is purely type-level with no emit impact.
Ambient Module Declarations: Compile-Time Trust Falls
Ambient module declarations are compile-time trust falls: you tell TypeScript the shape of code it cannot see. Use them for untyped npm packages, CSS imports, or CDN scripts.
ES Modules in TypeScript: The Compile-Time Contract
TypeScript ES modules are a compile-time target, not a runtime guarantee. Configure this when targeting modern browsers or Node with native ESM. The footgun is omitting .js extensions in imports, which it requires in ESM output even for .ts source files.
How does Rust differentiate unit and integration tests?
Tests Rust test layout and privacy. Unit tests sit in src/ under #[cfg(test)] and call private functions via super::. Integration tests go in tests/ as external crates. Wrong: claiming it blocks private testing or merging them into src/.
What does go mod tidy do beyond adding dependencies?
Tests reproducible Go module graph knowledge. A strong answer covers that tidy reconciles imports with go.mod, prunes unused modules, and ensures go.sum contains every checksum for the minimal build list.
Explain the difference between mod and use in Rust
Mod tells the compiler to compile a file into the crate tree; use brings an existing path into scope as a shortcut. declaration versus import in Rust's module system.
What is the purpose of the internal directory in Go?
Tests Go visibility boundaries beyond exported vs unexported. A strong answer states that internal is compiler-enforced module privacy, while lowercase is only package-private. Red flag: calling internal a naming convention rather than a build boundary.
Go package declaration, directory name, and import path relationship
Tests Go's separation of directory layout and package identity. A good answer states: import path is module path plus subdirectory; package clause is the in-code name; mismatch is legal and common for main or tests.
go.mod: Root of Go Module Identity
A go.mod file anchors a Go module, declaring its canonical path and dependencies to turn a directory into a versioned unit. Every project needs one at its root, and the path dictates how others import your packages.
Terraform Modules: Reusable Infrastructure Blueprints
A Terraform module is a reusable container for related resources, letting you stamp out infrastructure from one blueprint instead of copying HCL. Teams share VPC patterns or tagging standards with them.
Angular NgModules: Organizing Legacy Code
Think of an NgModule as a shipping container for related features, bundling components, directives, and pipes. You'll find them organizing code in older Angular apps.
TypeScript Namespaces: The Original Module System
TypeScript namespaces bundle related code under a single global name, preventing name collisions. They are useful for older projects or UMD library types, but the footgun is using them in modern apps; prefer standard ES modules.
Identifying JS Library Structure for TypeScript Types
To type a JS library, first identify its structure: module, global, or UMD. This dictates your .d.ts file's shape. You'll check docs for `import`/`require` or `<script>` usage. The footgun is misidentifying a UMD library, leading to incorrect import types.
export = : TypeScript's CommonJS Export Syntax
Think of `export =` as TypeScript's version of `module.exports`. It replaces a module's entire export with a single value, like a class or function. It's used for compatibility with CommonJS modules. The footgun is mixing it with standard `export`s.
Module Augmentation: Adding Types to External Libraries
Module augmentation is like monkey-patching for types, letting you add definitions to external modules. Use it to add a `user` property to Express's `Request` object.
Triple-Slash Directives: Compiler Hints in Comments
Triple-slash directives are compiler instructions inside comments, telling TypeScript about file dependencies. They're mostly seen in older projects or for global types, as modern `import` statements are preferred.
TypeScript: Type-Only Imports and Exports
Use `import type` to tell the compiler an import is only for type-checking and should be erased from the final JavaScript. This prevents tools like Babel from generating unwanted runtime code when compiling files in isolation.
Get Modules bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.