nodejs-best-practices

Node.js best practices including error handling, async patterns, security, and project structure.

⭐5stars🔀1forks📁View on GitHub🕐Updated Jan 11, 2026

When & Why to Use This Skill

This Claude skill provides a comprehensive framework for implementing Node.js best practices, focusing on scalable project architecture, robust error handling, and modern runtime features. It empowers developers to build secure, high-performance applications by leveraging native TypeScript support, standardized async patterns, and industry-standard security protocols.

Use Cases

  • Architecting scalable backend services using a modular structure (Controllers, Services, Repositories) to ensure long-term maintainability.
  • Implementing standardized, RFC 7807-compliant error handling to improve API debugging and provide consistent error responses to clients.
  • Modernizing development workflows by utilizing native Node.js 23+ features like the built-in test runner and experimental TypeScript stripping to reduce dependency overhead.
  • Enhancing application security through automated environment variable validation, rate limiting, and security header protection using Helmet.
  • Optimizing complex asynchronous workflows using advanced Promise patterns (all vs allSettled) to ensure system reliability and performance.
namenodejs-best-practices
descriptionNode.js best practices including error handling, async patterns, security, and project structure.

Node.js Best Practices

Project Structure

src/
├── controllers/    # HTTP request handlers
├── services/       # Business logic (Pure functions)
├── repositories/   # Data access (Drizzle/SQLite)
├── middleware/     # Hono/Fastify middleware
├── utils/          # AbortController & Stream helpers
├── config/         # Zod-validated configuration
└── index.ts        # Entry point (Native TS)

Error Handling

// Custom error classes
class AppError extends Error {
  constructor(
    public statusCode: number,
    public code: string,
    message: string
  ) {
    super(message);
  }
}

// Async error wrapper
const asyncHandler = (fn: RequestHandler) => (
  req: Request, res: Response, next: NextFunction
) => Promise.resolve(fn(req, res, next)).catch(next);

// Global error handler (RFC 7807 compliant)
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
  const statusCode = err instanceof AppError ? err.statusCode : 500;
  res.status(statusCode).json({
    type: "https://example.com/probs/" + (err instanceof AppError ? err.code : "internal-error"),
    title: err.message,
    status: statusCode
  });
});

Modern Runtime Features (Node.js 23+)

Native TypeScript (Strip Types)

Run TS files directly without transpile step:

node --experimental-strip-types src/index.ts

Built-in Test Runner

import { test, describe } from 'node:test';
import assert from 'node:assert';

test('business logic', async () => {
  assert.strictEqual(add(1, 2), 3);
});

Native SQLite

import { DatabaseSync } from 'node:sqlite';
const db = new DatabaseSync('local.db');
db.exec("CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT)");

Async Patterns

// Promise.all for parallel operations
const [users, posts] = await Promise.all([
  userRepo.findAll(),
  postRepo.findAll()
]);

// Promise.allSettled for non-critical failures
const results = await Promise.allSettled([
  sendEmail(user),
  updateAnalytics(user)
]);

Environment Variables

// Validate on startup
const requiredEnvVars = ['DATABASE_URL', 'JWT_SECRET'];
for (const envVar of requiredEnvVars) {
  if (!process.env[envVar]) {
    throw new Error(`Missing: ${envVar}`);
  }
}

Security

// Helmet for headers
app.use(helmet());

// Rate limiting
app.use(rateLimit({ windowMs: 15 * 60 * 1000, max: 100 }));

// Input validation
const schema = z.object({ email: z.string().email() });