Node Thapa Technical
Node Thapa Technical
Anyone can:
• View: You can see how Node.js is built under the hood.
• Modify: If you want to customize or improve Node.js, you
can do it.
• Contribute: Developers around the world actively
contribute to improving Node.js.
Supported platforms:
Windows macOS Linux
With Node.js:
Node.js allows JavaScript to run outside the browser, on the server.
It provides tools to interact with the system, like:
File system (read/write files).
Network (handle HTTP requests).
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
V8 Engine:
Node.js uses Google Chrome's V8 engine to
compile JavaScript into machine code, making it
lightning-fast.
Built-in APIs:
Node.js comes with built-in APIs (like fs for file
systems or http for servers) so you can build
powerful applications without extra libraries.
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Why is Node.js Special as a Runtime?
V8 Engine:
Node.js uses Google Chrome's V8 engine to
compile JavaScript into machine code, making it
lightning-fast.
Built-in APIs:
Node.js comes with built-in APIs (like fs for file
systems or http for servers) so you can build
powerful applications without extra libraries.
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Why Do We Need Node.js?
Single Language for Full Stack Development:
▪ Developers can use JavaScript for both frontend and backend, reducing complexity.
▪ For example, a MERN stack project (MongoDB, Express, React, Node).
High Performance:
▪ Thanks to the V8 engine, Node.js is super fast.
▪ It can handle thousands of simultaneous connections with ease.
backend applications.
History and Evolution
• npm (2010): Node.js launched its package manager
(npm) in 2010, revolutionizing JavaScript
development by simplifying package sharing and
dependency management.
development
ecosystem.
Some Famous npm Packages
• Express.js: A popular framework for building web applications and APIs.
• React (and React-DOM): Though originally developed for the browser, React
can be installed via npm for building modern web applications.
• The actual merger took place in September 2015, resulting in the release
of Node.js v4.0. This version combined the best features of both projects,
including V8 ES6 support from io.js, and marked the beginning of a Long-
Term Support (LTS) release cycle for Node.js.
HTML
CSS
JavaScript
JavaScript
Download Node.js
Download Node.js
• Prettier
•THAPA
TurnTECHNICAL
on Format on Save
– NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Installing Node.js and npm (FNM)
• You can download Node.js from its official website. We
will be using FNM in this course.
Each part of the acronym highlights a specific step in how the REPL works:
• Read: The REPL reads the user’s input (a single line or multiple lines of code) and
parses it into a data structure that the JavaScript engine can understand.
• Eval (Evaluate): The parsed input is evaluated (executed) by the JavaScript engine. If
the input is a valid expression, the REPL computes the result.
• Print: The result of the evaluated expression is printed back to the console so the user
can see the output.
• Loop: The process then loops back, waiting for the next input, and continues until the
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
user exits.
Node.js Basics
Understanding the Node.js Module System
Creating and Organizing Modules
Common Node.js Built-in Modules
Streams and Buffers
Introduction to package.json
ES Modules in Node.js THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Creating Your First
"Hello World"
Application
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
What is CLI?
• CLI (Command Line Interface) is a way to execute predefined JavaScript files or
run commands for tasks like installing packages, running scripts, or starting
applications.
• Automation: Use npm scripts for repetitive tasks (e.g., building or testing code).
• Large-Scale Projects: Execute full files for consistent and reusable logic.
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Windows vs Global
in Node.js
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Did you know that JavaScript
behaves differently in the
browser and in Node.js?
• Instead, Node.js has a global object. It's the equivalent of window in the
browser but designed for a server-side environment.
Why is it useful?
• Consistent Access: In the past, accessing the global object varied depending on the
environment:
1. Browser: window
2. Node.js: global
3. Web Workers: self
4. Other Environments: Might have their own global objects
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Node.js Module
• Variables, functions, or objects defined in one file are not accessible in another
file by default unless you explicitly export them.
Encapsulation:
• You must export anything you want to make accessible to other modules.
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Creating
and
Organizing
Modules
__filename
__dirname
path.join(): Joins multiple path segments into one, using the appropriate
separator (\ on Windows, / on Linux/macOS).
path.basename(): Returns the last part of a path (e.g., file name with extension).
1: crypto.randomBytes(size)
2. crypto.createHash(algorithm)
Purpose: Creates a hash for a given input using algorithms like sha256.
▪ Example:
▪ A user clicks "Forgot Password" → A random token is sent via email → The token is verified
during the reset process.
▪ Example:
▪ When creating a developer account on platforms like Twitter or GitHub, a unique API key is
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
generated.
crypto.createHash(algorithm)
Real-Life Examples:
▪ Example:
▪ A user’s password is hashed with sha256 and stored. During login, the
entered password is hashed and compared.
• fs (File System): Handles file operations like reading, writing, and deleting files.
• path: Simplifies working with file and directory paths.
• os (Operating System): Provides information about the operating system and
hardware.
• crypto: Offers tools for encryption, hashing, and secure token generation.
• http/https: Allows creation of web servers and handling of HTTP/HTTPS
requests.
• events: Supports an event-driven programming model with emitters and
listeners.
You can also checkout Node.js official website to see all built-in modules.
FS Module
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Thapa Technical
FS Module - Node.js
• The fs (File System) module in Node.js is a core module that allows you
to work with the file system, enabling you to read, write, update, delete,
and watch files.
Read (fs.readFile)
• Reads the content of the file asynchronously and logs it.
Update (fs.appendFile)
• Appends new content to the file without overwriting the existing content.
Delete (fs.unlink)
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Use Cases
Reading Files asynchronously in a non-blocking way.
Writing or Appending Data to files without blocking the event loop.
Performing Multiple File Operations sequentially or concurrently with
promise chaining.
Handling Errors Gracefully with .catch()
THAPA TECHNICAL or try...catch
– NODEJS YOUTBEblocks.
SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Event Module
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Thapa Technical
Event Module - Node.js
EventEmitter is a core module in Node.js used to create and handle custom events.
It is part of the events module and is often used for building event-driven systems in
Node.js.
Key Methods
1. emit(eventName, [args])
Purpose: Emits (or triggers) an event with the specified eventName. You can also
pass arguments that will be consumed by the listeners.
It’s like calling a function, but instead, it triggers all listeners (functions) attached to
the specified event.
2. on(eventName, listener)
THAPA TECHNICAL to a –specific
NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
Imagine, you have a truck full of bricks that you want to carry.
Streams and Buffers
But is this possible? Maybe, if you are a gorilla, but it’s not possible for a normal human being.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
So, what you will typically do is, take small chunk of bricks one by one and take it.
Streams and Buffers
16 GB RAM
8 GB RAM
16 GB RAM
8 GB RAM
2TB File
If a client wants to upload a 2 TB file to the server, then what it can do is give whole 2 TB file to server,
Then server can store it in disk. But is that possible?
Streams and Buffers
16 GB RAM
8 GB RAM
2TB File
It’s not possible because client first has to take whole 2 TB file in their RAM, then upload it to server, then server
should initially store whole 2 TB file in RAM, then store it in disk.
Streams and Buffers
16 GB RAM
8 GB RAM
2TB File
Neither client has 2 TB RAM, nor the server can handle it. In that case, we have to split the file
Into chunks and server should take the file in chunk and write into disk at the same time.
Streams and Buffers
16 GB RAM
8 GB RAM
2TB File
now.
HTTP MODULE
• Scripts:
• Contains custom scripts for various tasks (e.g., running tests, starting the server).
• Provides shortcuts for common development tasks.
• ES Modules (ECMAScript Modules) allow you to use import and export syntax.
They have been available in Node.js since version 12.
• To enable ES Modules, you can either:
• Name your file with the .mjs extension, or
• Set "type": "module" in your package.json. (Recommended)
• Use import and export instead of require and module.exports.
• After Node.js v14.8, you can use top-level await when ES Modules are enabled.
• We'll use ES Modules in this course as it's part of the ECMAScript standard and
a more modern approach. Don’t worry, even if a package uses CommonJS, you
can import it into ES Modules. However, importing ES Module packages into
CommonJS may cause THAPAissues, as some
TECHNICAL packages
– NODEJS YOUTBE have switched exclusively to
SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
ES Modules.
ES Modules in Node.js
Introduction to npm
Introduction to npm
• While importing, first Node.js checks for core modules, then files or folders, and
at last looks inside node_modules.
Semantic Versioning System
Most of the npm packages use Semantic Versioning System or SemVer. Note: Some packages
like typescript, react-native don’t follow it.
Symbols in dependency versions
Symbol Meaning Example Resolves To
^ (Caret) Minor and patch updates allowed ^4.17.1 4.18.0, not 5.0.0
• npm update
• This updates all the packages in your project, but it follows the range defined in
package.json. It doesn’t update to absolute latest version.
• npx npm-check-updates
• npx is a CLI tool that comes with npm.
• It is used to execute a package without requiring you to install globally or locally.
• It is useful for temporary usage of a package.
• npm-check-updates is a package which you can use to upgrade your packages to
absolute latest versions.
• Use -u flag at the end to update the packages after reviewing.
• This only updates package.json, then you can use npm install to update the
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES
packages.
Global packages
• Up until now, we have been installing packages locally.
• To install package globally,
• npm install -g npm-check-updates
• On Linux, and macOS, you might have to prefix it with sudo
• This package gets installed globally, and you can access from anywhere.
• You can try running npm-check-updates command anywhere in any projects without using npx.
• npm-check-updates also have an alias named ncu, which you can also use.
• Fun fact: npm is itself a global package which means if you need to update npm, you
will do: npm install -g npm
• npm outdated -g
• To see outdated global packages.
• npm update –g <package-name>
THAPA TECHNICAL – NODEJS YOUTBE SERIES
THAPA TECHNICAL – NODEJS YOUTBE SERIES