DEV Community

Cover image for The Ultimate Node.js Cheat Sheet for Developers
Dipak Ahirav
Dipak Ahirav

Posted on • Edited on

The Ultimate Node.js Cheat Sheet for Developers

πŸš€ Check Out My YouTube Channel! πŸš€

Hi everyone! If you enjoy my content here on Dev.to, please consider subscribing to my YouTube channel devDive with Dipak. I post practical full-stack development videos that complement my blog posts. Your support means a lot!

Welcome to the ultimate Node.js cheat sheet, your comprehensive guide to mastering Node.js, whether you're just starting out or looking to brush up on your knowledge. This post will cover installation, core modules, useful commands, and best practices for efficient Node.js development.

Getting Started with Node.js

Installing Node.js:
Download and install Node.js from nodejs.org. Choose the version recommended for most users, unless you have specific needs that require the latest features or earlier compatibility.

Verifying Installation:
Check that Node.js and npm (node package manager) were installed correctly by running:

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

Key Node.js Commands

  • Start a Node.js Application:
  node app.js
Enter fullscreen mode Exit fullscreen mode
  • Initialize a New Node.js Project:
  npm init -y
Enter fullscreen mode Exit fullscreen mode
  • Install a Package Locally:
  npm install package-name
Enter fullscreen mode Exit fullscreen mode
  • Install a Package Globally:
  npm install -g package-name
Enter fullscreen mode Exit fullscreen mode

Important Node.js Modules

File System (fs)

  • Read a file asynchronously:
  const fs = require('fs');
  fs.readFile('/path/to/file', 'utf8', (err, data) => {
    if (err) throw err;
    console.log(data);
  });
Enter fullscreen mode Exit fullscreen mode
  • Write to a file asynchronously:
  fs.writeFile('/path/to/file', 'Hello, world!', (err) => {
    if (err) throw err;
    console.log('File has been written!');
  });
Enter fullscreen mode Exit fullscreen mode

HTTP

  • Create an HTTP server:
  const http = require('http');
  const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');
  });

  server.listen(3000, () => {
    console.log('Server running at http://localhost:3000/');
  });
Enter fullscreen mode Exit fullscreen mode

Path

  • Manage file paths:
  const path = require('path');
  console.log(path.resolve('app.js'));  // Resolves to absolute path
Enter fullscreen mode Exit fullscreen mode

Debugging in Node.js

Basic Debugging:
Use console.log, console.error, and console.warn to output debugging information to the console.

Advanced Debugging:
Run Node.js in inspect mode and attach a debugger.

node --inspect-brk app.js
Enter fullscreen mode Exit fullscreen mode

Environment Management

Setting Environment Variables:

export NODE_ENV=production
Enter fullscreen mode Exit fullscreen mode

Accessing Environment Variables in Node.js:

console.log(process.env.NODE_ENV);
Enter fullscreen mode Exit fullscreen mode

Working with npm

  • Update a Package:
  npm update package-name
Enter fullscreen mode Exit fullscreen mode
  • List Installed Packages:
  npm list
Enter fullscreen mode Exit fullscreen mode
  • Uninstall a Package:
  npm uninstall package-name
Enter fullscreen mode Exit fullscreen mode

Conclusion

This Node.js cheat sheet is designed to help you quickly find the command or code snippet you need to enhance your development workflow. Whether you're troubleshooting, setting up a new project, or learning the ropes, keep this guide handy to streamline your Node.js experience.

Stay curious and keep building amazing things with Node.js!

Top comments (0)