0.Node

Node.js Overview


Setting Up a Node.js Project

  1. Open a folder in Visual Studio Code.
  2. Initialize npm (Node Package Manager):
    • Run the command:
      npm init  # or use npm init -y for default settings
      
    • This command creates a package.json file in the project directory, which manages the project dependencies and metadata.

In Node.js, EJS and ESM refer to two different concepts that often get mixed up due to their similar acronyms:

  1. EJS (Embedded JavaScript):

    • EJS is a templating language that lets you generate HTML using JavaScript code. It’s useful when you want to create dynamic HTML pages.
    • You can insert JavaScript code directly in your HTML by using <% %> tags.
    • Example: It’s often used in Express apps for rendering HTML pages with dynamic content.
  2. ESM (ECMAScript Modules):

    • ESM is a standard way to handle JavaScript modules. It allows you to import and export code between different files using import and export statements.
    • ESM is the modern module format in JavaScript and is now natively supported in Node.js (with .mjs files or by setting "type": "module" in package.json).
    • Example: ESM helps organize code by separating it into smaller modules, which makes it reusable and easier to manage.

Key Difference:

cjs

../DataBase/attachments/Pasted image 20241028235734.png| 700

ESM

../DataBase/attachments/Pasted image 20241028235810.png

https://nodejs.org/docs/latest/api/fs.html


Npm scripts (run/test)

../DataBase/attachments/Pasted image 20241031194335.png