Express.js Mastery / Introduction To Node.js And NPM

Core Modules

๐Ÿ› ๏ธ Prerequisites

Make sure the 1-node-farm starter folder is open. If you're currently in the project root, please open that folder instead.


๐Ÿ“ Step 1: Create a New JavaScript File

  1. In the root folder of your project, create a new file named:

index.js

โœ๏ธ Step 2: Write Your First Node Code

Inside index.js, write:

const hello="Hello world!";
console.log(hello);
  • This creates a variable called hello storing the classic "Hello world" text
    (traditionally the first thing written when learning any new programming language).

  • console.log() prints the value to the terminal.

  • Save the file.

โ–ถ๏ธ Step 3: Run the File Using Node

  • Unlike browser JavaScript (where you'd link the file inside an HTML page and open it in a browser), Node lets you run JavaScript files directly from the terminal.

  • In the terminal, type:

node index.js

and press Enter.

  • Important: don't just type node and press Enter alone โ€” that opens the REPL instead. You must include the filename right after node.

  • Output:

Hello world!

๐ŸŽ‰ This means you've successfully run your first Node.js script.


๐Ÿง Understanding Node Modules

  • Node.js can do things browser JavaScript cannot โ€” like reading and writing files from the computer's file system.

  • This functionality is not built into plain JavaScript โ€” it comes from Node modules.

  • For file operations, Node provides a built-in module called FS (File System).

๐Ÿ“ฅ Importing (Requiring) a Module

To use a module, you require it and store the result in a variable:

const fs = require('fs');
  • require('fs') loads the File System module.

  • The result (an object containing many useful functions for reading/writing files) is stored in the fs variable.

  • This fs variable will be used in the next step of the course to actually read files.


๐Ÿ“š Learning to Use the Node Documentation

Whenever you need info about a module or function, refer to the official Node.js documentation:

  1. ๐ŸŒ Go to nodejs.org

  2. ๐Ÿ“– Click the Documentation tab.

  1. ๐Ÿ“‚ On the left side, you'll see a list of all built-in modules, such as:

    • ๐Ÿ–ฅ๏ธ REPL (used earlier)

    • ๐Ÿ“ File System (fs) (used in this note)

    • ๐Ÿ“ฆ Many others


๐Ÿ“– Reading the Docs

  • Each module page covers its functions, parameters, and examples.

  • Example: to read a file, look up fs.readFileSync() โ€” we'll use this next.

  • It's okay if the docs feel confusing at first; it'll click as you go.


๐ŸŽฏ Key Takeaways

  • You can write and run standalone .js files using the command:

node filename.js
  • Node's extra capabilities (like file handling) come from modules, which you load using require().

  • The official Node.js documentation is the go-to resource for learning about any built-in module or function.

  • Next step: Actually use the fs module to read a file from disk using fs.readFileSync().