DeveloperBreeze

Section 1.2: Setting Up the Environment

Tutorial 1.2.3: Using Node.js to Run JavaScript


Using Node.js to Run JavaScript

Node.js is a JavaScript runtime that allows you to execute JavaScript code outside of the browser. It's an essential tool for modern development, especially for server-side applications and command-line scripts. This tutorial will guide you through installing Node.js and running JavaScript with it.


Why Use Node.js?

  • Run JavaScript Anywhere: Execute code on your computer, independent of the browser.
  • Server-Side Development: Build APIs and back-end services.
  • Access to npm: Use and manage libraries and tools through the Node Package Manager (npm).

Step-by-Step Guide

  1. Download Node.js:
  1. Install Node.js:
  • Run the downloaded installer and follow the installation prompts.
  • Ensure that npm (Node Package Manager) is installed alongside Node.js (it usually is).
  1. Verify Installation:
  • Open your terminal (Command Prompt, PowerShell, or any terminal on macOS/Linux).
  • Check the Node.js version:
     node -v
  • Check the npm version:
     npm -v

Running JavaScript with Node.js

  1. Using the REPL (Read-Eval-Print Loop):
  • Open the terminal and type node to enter the Node.js interactive mode.
  • Type JavaScript directly:
     > console.log("Hello, Node.js!");
     Hello, Node.js!
  1. Running a JavaScript File:
  • Create a new file named example.js and add the following code:
     console.log("Running JavaScript with Node.js!");
  • Save the file and run it with Node.js:
     node example.js
  • Output:
     Running JavaScript with Node.js!

Exploring Node.js Features

  1. Accessing the File System:
  • Example of reading a file:
     const fs = require('fs');
     fs.readFile('example.txt', 'utf8', (err, data) => {
       if (err) throw err;
       console.log(data);
     });
  1. Using npm:
  • Install a package:
     npm install lodash
  • Use it in your code:
     const _ = require('lodash');
     console.log(_.capitalize("hello world"));

Benefits of Using Node.js for JavaScript

  • Fast Execution: Powered by Google’s V8 engine.
  • Asynchronous and Event-Driven: Ideal for non-blocking applications.
  • Cross-Platform: Runs on Windows, macOS, and Linux.

Continue Reading

Discover more amazing content handpicked just for you

Tutorial

Build a Custom Rate Limiter in Node.js with Redis

// redisClient.js
const redis = require("redis");

const client = redis.createClient({ url: process.env.REDIS_URL });

client.on("error", (err) => console.error("Redis error:", err));
client.connect();

module.exports = client;
// rateLimiter.js
const client = require("./redisClient");

const rateLimiter = (limit = 100, windowSec = 3600) => {
  return async (req, res, next) => {
    const ip = req.ip;
    const key = `rate_limit:${ip}`;

    const current = await client.get(key);

    if (current !== null && parseInt(current) >= limit) {
      return res.status(429).json({ error: "Too many requests. Try later." });
    }

    const multi = client.multi();
    multi.incr(key);
    if (!current) {
      multi.expire(key, windowSec);
    }
    await multi.exec();

    next();
  };
};

module.exports = rateLimiter;

Apr 04, 2025
Read More
Tutorial
javascript

JavaScript in Modern Web Development

JavaScript is the engine behind the dynamic behavior of modern websites. It works alongside HTML (structure) and CSS (style) to create a complete web experience.

  • JavaScript enables features like dropdown menus, modal windows, sliders, and real-time updates.
  • Examples: Search suggestions, form validations, chat applications.

Dec 10, 2024
Read More
Tutorial

Connecting a Node.js Application to an SQLite Database Using sqlite3

Key Takeaways:

  • Simplicity: SQLite's serverless architecture simplifies database management.
  • Efficiency: sqlite3 provides a robust API to perform complex SQL operations seamlessly.
  • Security: Always prioritize the security of your data by following best practices.

Oct 24, 2024
Read More
Tutorial
bash

How to Update Node.js and npm on Ubuntu

npm cache clean --force

If you're working on a Node.js project, you might want to reinstall your project's dependencies to ensure compatibility with the updated Node.js version. Navigate to your project directory and run the following commands:

Oct 03, 2024
Read More
Tutorial
javascript

Creating a Component Library with Storybook and React

npx create-react-app my-component-library
cd my-component-library

Once your React app is set up, we can start integrating Storybook.

Aug 27, 2024
Read More
Cheatsheet

Front-End Development Tools and Libraries Cheatsheet

No preview available for this content.

Aug 21, 2024
Read More
Tutorial
javascript

Integrating Vite with React in a Laravel Project: A Comprehensive Guide

   composer create-project laravel/laravel my-react-laravel-app

Navigate into your project directory:

Aug 14, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!