Skip to content

thedaviddias/Front-End-Checklist / skills/es-modules

es-modules

Verified

Use when reviewing scripts, client components, bundles, or runtime behavior related to Use ES modules (import/export). Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.

FileKindSize
SKILL.mdskill md1.9 KB
references/rule.mdmarkdown3.1 KB

references/rule.md

View on GitHub
# Use ES modules (import/export)

> Use native ES module syntax for imports and exports instead of CommonJS require() to enable static analysis, tree-shaking, and better tooling support.

**Priority:** high · **Difficulty:** beginner · **Time:** 15 min

---
ES modules (`import`/`export`) are the JavaScript standard for code organization and are supported natively in all modern browsers and Node.js.

## Code Example

```javascript
// ❌ CommonJS (avoid for new code)
const utils = require('./utils')
const { formatDate } = require('./utils')
module.exports = { myFunction }
module.exports = myFunction

// ✅ ES Modules

```

## Why It Matters

ES modules are statically analyzable — bundlers can determine at build time what code is actually used and eliminate the rest (tree-shaking). CommonJS require() is dynamic and prevents this optimization. ES modules are also the browser-native standard, reducing the need for build-time transformation.

## Named vs Default Exports

```javascript
// utils.js — named exports (preferred for libraries and utilities)

// consumer.js

// Only imported functions are included in the bundle
```

```javascript
// UserCard.js — default export (common for single-concept modules)

// consumer.js

```

## In the Browser

```html
<!-- Add type="module" to use import/export directly -->
<script type="module">
  import { formatDate } from './utils.js'
  console.log(formatDate(new Date()))
</script>
```

Module scripts are deferred by default, execute in strict mode, and have their own scope (no global leakage).

## Dynamic Imports for Code Splitting

```javascript
// Load a heavy module only when needed
async function loadChart() {
  const { Chart } = await import('./chart.js')
  return new Chart(document.getElementById('canvas'))
}

button.addEventListener('click', loadChart)
```

## Re-exporting (Barrel Files)

```javascript
// index.js — re-export from a central entry point

```

## Standards

- Use MDN: JavaScript Guide as the standard for how this JavaScript pattern should behave in production, not just in a small local example.
- Use web.dev: Learn JavaScript as the standard for how this JavaScript pattern should behave in production, not just in a small local example.

## Support Notes

- Module loading behavior depends on the target browser matrix and build pipeline, so verify the final shipped output rather than relying only on source syntax.
- If legacy targets remain in scope, document the fallback or transpilation path explicitly.
- ES modules and class bodies run in strict mode automatically. If the codebase still ships classic non-module scripts, an explicit `'use strict'` directive can still matter there.

## Verification

### Automated Checks

- Verify the behavior in the browser after the code change, not only in static analysis.
- Inspect DevTools Network or Performance panels when the rule affects loading or execution order.
- Test the primary user flow and one edge case triggered by the changed script path.

### Manual Checks

- Confirm the code still behaves correctly when the feature is delayed, lazy-loaded, or fails.
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall