Js module pattern

From Intense-Visions/harness-engineering · View interactive version

Js module pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Encapsulate private state and expose a public API using closures or ES modules

How to use this skill

Skill file

JS Module Pattern

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

When to Use

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.
// 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,
  };
})();
  1. 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:

When NOT to use:

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

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.