Skip to content
Back to skills

Js Module Pattern

ASecurity

> Encapsulate private state and expose a public API using closures or ES modules

  • 20 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 11, 2026
developmentjavascriptjavaexpressapi

Works with

  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned September 11, 2026

npx -y skills add Intense-Visions/harness-engineering --skill js-module-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Js Module Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Js Module Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/intense-visions-js-module-pattern/badge)](https://www.skillsdirectory.com/skills/intense-visions-js-module-pattern)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# JS Module Pattern

> Encapsulate private state and expose a public API using closures or ES modules

## When to Use

- You need private variables that are not accessible from outside a module
- You want a clean public API with implementation details hidden
- You are working in a pre-ESM environment where IIFE-based modules were the norm

## Instructions

1. In modern code (ESM), declare module-level variables as private by not exporting them. Export only the public API.
2. In legacy or bundled code, use an IIFE to create a closure scope.
3. Freeze exported objects if they should be immutable.
4. Keep modules focused — one module per concern.

```javascript
// Modern ESM module pattern
let _count = 0; // private — not exported

export function increment() {
  _count++;
}

export function getCount() {
  return _count;
}

// Legacy IIFE module pattern
const counter = (() => {
  let _count = 0;
  return {
    increment: () => _count++,
    getCount: () => _count,
  };
})();
```

5. Prefer ESM named exports over default exports for better tree-shaking.

## Details

The Module pattern predates ES modules. In the browser, there was no built-in module system, so developers used IIFEs (Immediately Invoked Function Expressions) to create private scopes. Today, ES modules (`.mjs`, `type="module"`) provide native module semantics — each file gets its own scope.

**Trade-offs:**

- Module-level state is shared across all importers in the same process — it is a singleton
- IIFE modules are not tree-shakeable by bundlers; prefer named ESM exports
- Circular module dependencies can cause initialization order issues

**When NOT to use:**

- When you need per-instance private state — use classes with private fields (`#field`)
- When the module has no state — just export pure functions directly

## Source

https://patterns.dev/javascript/module-pattern

## Process

1. Read the instructions and examples in this document.
2. Apply the patterns to your implementation, adapting to your specific context.
3. Verify your implementation against the details and edge cases listed above.

## Harness Integration

- **Type:** knowledge — this skill is a reference document, not a procedural workflow.
- **No tools or state** — consumed as context by other skills and agents.

## Success Criteria

- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.

Files in this skill

  • SKILL.md2.4 KB
  • skill.yaml674 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…