Cheatsheet#

A quick reference guide for common Node.js commands, patterns, and modules.

πŸ‘‰ New to App-Generator? Join our 10k+ Community using GitHub One-Click SignIN.

Basic Commands#

Check Node.js and npm Versions

node -v
npm -v

Initialize a New Project

npm init -y

Install a Package

npm install <package-name>

Built-in Modules#

File System (fs)

Reading a file:

const fs = require('fs');
fs.readFile('file.txt', 'utf8', (err, data) => {
    if (err) throw err;
    console.log(data);
});

Writing to a file:

fs.writeFile('file.txt', 'Hello, Node.js', (err) => {
    if (err) throw err;
    console.log('File written!');
});

HTTP Module

Creating a basic 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, '127.0.0.1', () => {
    console.log('Server running at http://127.0.0.1:3000/');
});

Third-party Modules#

Node.js projects commonly use third-party modules to add functionality, often installed through npm.

Here’s how to work with third-party modules:

Importing Modules

Install a module with npm, then import it in your project using require():

npm install <module-name>
const dotenv = require('dotenv');
dotenv.config();

Exporting Custom Modules

Define and export custom functions or objects from your modules using module.exports:

// In myModule.js
const myFunction = () => {
    console.log('Hello from myModule!');
};

module.exports = myFunction;

Import it in another file:

const myFunction = require('./myModule');
myFunction();

NPM Scripts#

Run a Script

Add a script in package.json:

"scripts": {
    "start": "node app.js"
}

Run the script:

npm start

Environment Variables#

Environment variables are ideal for storing configuration and sensitive data in a NodeJS project.

Use process.env to access environment variables.

const port = process.env.PORT || 3000;
console.log(`Server will run on port ${port}`);

To load variables from a .env file, install and use the dotenv package:

npm install dotenv

Load the .env file at the top of the file where you need it.

require('dotenv').config();

Error Handling#

Try-Catch Block

try {
    // Code that may throw an error
} catch (error) {
    console.error('Error:', error);
}

Promise Handling

Using .then() and .catch():

someAsyncFunction()
    .then(result => console.log(result))
    .catch(error => console.error(error));

Using async/await:

async function asyncCall() {
    try {
        const result = await someAsyncFunction();
        console.log(result);
    } catch (error) {
        console.error(error);
    }
}

Best Practices#

  • Always use environment variables for configuration and sensitive data.

  • Handle errors gracefully.

  • Follow asynchronous programming patterns.

  • Keep modules small and focused.