npx agentspec-cli install a41aa73d-eb84-406d-a13b-3b878d34fa6b --file-type cursor-rules→ .cursor/rules/
---
description: Node.js Patterns
alwaysApply: false
---
# Node.js Patterns
Production Node.js patterns for services, CLIs, and tooling.
## Graceful Shutdown
```typescript
const shutdown = async (signal: string) => {
process.stdout.write(`Received ${signal}, shutting down...`);
server.close();
await db.disconnect();
process.exit(0);
};
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('unhandledRejection', (reason) => { console.error('Unhandled:', reason); process.exit(1); });
```
## Streams
```typescript
import { pipeline } from 'node:stream/promises';
// Always use pipeline for proper error handling and cleanup
await pipeline(
createReadStream('input.log'),
new Transform({ transform(chunk, _, cb) {
cb(null, chunk.toString().split('\n').filter((l: string) => l.includes('ERROR')).join('\n'));
}}),
createGzip(),
createWriteStream('errors.log.gz'),
);
```
## Worker Threads
```typescript
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads';
if (isMainThread) {
const runWorker = <T>(data: unknown): Promise<T> =>
new Promise((resolve, reject) => {
const worker = new Worker(new URL(import.meta.url), { workerData: data });
worker.on('message', resolve);
worker.on('error', reject);
});
} else {
parentPort!.postMessage(performExpensiveComputation(workerData));
}
```
## Configuration
```typescript
import { z } from 'zod';
const EnvSchema = z.object({
NODE_ENV: z.enum(['development', 'production', 'test']),
PORT: z.coerce.number().int().positive().default(3000),
DATABASE_URL: z.string().url(),
});
export const env = EnvSchema.parse(process.env); // Crash at startup if invalid
```
## Module System
- Use `node:` protocol for builtins (`import { readFile } from 'node:fs/promises'`)
- ESM by default — `"type": "module"`, use `.js` extensions in imports
- Always use `fs/promises`, never sync variants in async code
## Anti-Patterns
- Never `require()` in ESM — use `import` or `createRequire`
- Never unbounded concurrency — use `p-limit` or manual batching
- Never `process.exit()` without cleanup or string concat for SQL
aws-cloud-patterns
AWS cloud patterns for Lambda, ECS, S3, DynamoDB, and Infrastructure as Code with CDK/Terraform
by AgentHub Crawler
Patterns
Use `agent-browser` for web automation. Run `agent-browser --help` for all commands. 1. `agent-browser open <url>` - Navigate to page 2. `agent-browser snapshot -i` - Get interactive elements with refs (@e1, @e2)
by AgentHub Crawler
Secure Node.js Development
These rules apply to all Node.js code in the repository and aim to prevent common security risks through disciplined use of input validation, output encoding, and safe APIs. All violations must include a clear explanation of which rule was triggered and why, to help developers understand and fix the issue effectively.
by AgentHub Crawler
Sign in to leave a comment.
Node.js and Express.js Best Practices
Cursor IDE rules and conventions for AI-assisted development.
by AgentHub Crawler