The Ultimate Node.js Cheat Sheet for Developers
Tags: javascript, node, cheatsheet
Published: 2024-04-26 10:04:21

## 🚀 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](https://www.youtube.com/@DevDivewithDipak). 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](https://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:
```bash
node -v
npm -v
```

### Key Node.js Commands

- **Start a Node.js Application**:
  ```bash
  node app.js
  ```

- **Initialize a New Node.js Project**:
  ```bash
  npm init -y
  ```

- **Install a Package Locally**:
  ```bash
  npm install package-name
  ```

- **Install a Package Globally**:
  ```bash
  npm install -g package-name
  ```

### Important Node.js Modules

#### File System (fs)

- **Read a file asynchronously**:
  ```javascript
  const fs = require('fs');
  fs.readFile('/path/to/file', 'utf8', (err, data) => {
    if (err) throw err;
    console.log(data);
  });
  ```

- **Write to a file asynchronously**:
  ```javascript
  fs.writeFile('/path/to/file', 'Hello, world!', (err) => {
    if (err) throw err;
    console.log('File has been written!');
  });
  ```

#### HTTP

- **Create an HTTP server**:
  ```javascript
  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/');
  });
  ```

#### Path

- **Manage file paths**:
  ```javascript
  const path = require('path');
  console.log(path.resolve('app.js'));  // Resolves to absolute path
  ```

### 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.
```bash
node --inspect-brk app.js
```

### Environment Management

**Setting Environment Variables**:
```bash
export NODE_ENV=production
```

**Accessing Environment Variables in Node.js**:
```javascript
console.log(process.env.NODE_ENV);
```

### Working with npm

- **Update a Package**:
  ```bash
  npm update package-name
  ```

- **List Installed Packages**:
  ```bash
  npm list
  ```

- **Uninstall a Package**:
  ```bash
  npm uninstall package-name
  ```

### 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!