Skip to content

goldbergyoni/nodebestpractices

Repository files navigation

Node.js Best Practices

Node.js Best Practices


102 items Last update: January 3rd, 2024 Updated for Node 22.0.0

Follow us on Twitter! @nodepractices


Read in a different language: CNCN, FRFR, BRBR, RURU, PLPL, JAJA, EUEU (ESES, HEHE, KRKR and TRTR in progress! )


🎊 2024 edition is here!

  • 🛰 Modernized to 2024: Tons of text edits, new recommended libraries, and some new best practices

  • ✨ Easily focus on new content: Already visited before? Search for #new or #updated tags for new content only

  • 🔖 Curious to see examples? We have a starter: Visit Practica.js, our application example and boilerplate (beta) to see some practices in action



Welcome! 3 Things You Ought To Know First

1. You are reading dozens of the best Node.js articles - this repository is a summary and curation of the top-ranked content on Node.js best practices, as well as content written here by collaborators

2. It is the largest compilation, and it is growing every week - currently, more than 80 best practices, style guides, and architectural tips are presented. New issues and pull requests are created every day to keep this live book updated. We'd love to see you contributing here, whether that is fixing code mistakes, helping with translations, or suggesting brilliant new ideas. See our writing guidelines here

3. Best practices have additional info - most bullets include a 🔗Read More link that expands on the practice with code examples, quotes from selected blogs, and more information



By Yoni Goldberg

Learn with me: As a consultant, I engage with worldwide teams on various activities like workshops and code reviews. 🎉AND... Hold on, I've just launched my beyond-the-basics testing course, which is on a 🎁 limited-time sale until August 7th



Table of Contents

1. Project Architecture Practices (6)

  1.1 Structure your solution by components #strategic #updated
  1.2 Layer your components, keep the web layer within its boundaries #strategic #updated
  1.3 Wrap common utilities as packages, consider publishing
  1.4 Use environment aware, secure and hierarchical config #updated
  1.5 Consider all the consequences when choosing the main framework #new
  1.6 Use TypeScript sparingly and thoughtfully #new

2. Error Handling Practices (12)

  2.1 Use Async-Await or promises for async error handling
  2.2 Extend the built-in Error object #strategic #updated
  2.3 Distinguish operational vs programmer errors #strategic #updated
  2.4 Handle errors centrally, not within a middleware #strategic
  2.5 Document API errors using OpenAPI or GraphQL
  2.6 Exit the process gracefully when a stranger comes to town #strategic
  2.7 Use a mature logger to increase errors visibility #updated
  2.8 Test error flows using your favorite test framework #updated
  2.9 Discover errors and downtime using APM products
  2.10 Catch unhandled promise rejections #updated
  2.11 Fail fast, validate arguments using a dedicated library
  2.12 Always await promises before returning to avoid a partial stacktrace #new
  2.13 Subscribe to event emitters 'error' event #new

3. Code Style Practices (12)

  3.1 Use ESLint #strategic
  3.2 Use Node.js eslint extension plugins #updated
  3.3 Start a Codeblock's Curly Braces on the Same Line
  3.4 Separate your statements properly
  3.5 Name your functions
  3.6 Use naming conventions for variables, constants, functions and classes
  3.7 Prefer const over let. Ditch the var
  3.8 Require modules first, not inside functions
  3.9 Set an explicit entry point to a module/folder #updated
  3.10 Use the === operator