Node.
Js
Local and Export Modules
[Link] File system -
To check wether [Link] installed in
your system or not.
In Visual studio we have already installed an Extension , Still need to install few
more extensions
Install Prettier code formatter
After installing prettier code
Install Vscode -icons
After Installing VS code Icons
Live server installation
After Installing Live server
Install Code runner
Now go back to explorer
Click file open folder
Create a New folder
Created a new folder
Select the folder
Create a new java script
file …
Write a simple javascript code
Click Terminal and open
New Terminal
It shows complete path of the folder
When you give ls command it will
show the files in it .
Just by giving node keyword and js file name (server )
You will get the output
"In [Link], console output that would typically be printed in the browser's console (when using JavaScript in the
browser) is instead printed in the terminal."
• If you're working with frontend (React, Vanilla JS, etc.) → Browser Console is best
• If you're working with backend ([Link], Express, Databases) → Terminal Console is the best
Just by giving node
command you will
enter into the node
environment .
You can do basic
operations like python
Press Ctrl + e
to exit
Use the built-in os module in [Link] to retrieve system-
related information.
\\Import the os module
Execute the J.S file
You can see all
the os related
information
When you need
system paths, like the
home directory or
temp folder use os
module
It gives the Dir name of
the Server j.s file located
It gives the Dir name of the Server
j.s file along with the file name
This line imports the built-in path module in [Link], which is used for handling and manipulating file and
directory paths.
// Get the directory name
// Get the file name
// Get the file extension
When you need to
construct, modify, or
resolve file [Link]
path module
Create a New File [Link]
We imported the math module to
[Link]
We can see the Add function is
executed here
Output when calling all the functions
This is another way of importing the math module and calling the
functions……
This is another way of exporting the function directly.
Stil we will get the same output ……….
In the official website of nodejs we will get the documents
particular to our version
Choose the version you
installed in your system
We can see lot of modules
available.
We already worked with some
basic functions in OS and Path
module.
We can explore a lot about these Modules and functions in it
……………….
We can see lot of
Classes and
Functions in File
system to explore
When I searched for Read I can see lot of functions related to Read operation
From this iam choosing
[Link]
We can get some code
snippets for that
[Link] function
Create a folder and give a Name to it
Inside that Folder create a
text file
Enter a text of your choice
Copy this code and paste it in VS
javascript file
err (Error Object) → If an error occurs (e.g., file not found), this will contain the error details.
If no error occurs, err will be null.
This is the code we copied from
the document
data (Buffer/String) → If the file is read successfully, this contains the file's content.
If no encoding is specified, data will be a Buffer; otherwise, it will be a string.
Give the details of the Folder and file from
which we need to read the data .
The readFile function read the content
of the file as buffer by default.
toString() method is used to convert this buffer into a readable string.
We can specify the encoding while reading
Data is already a string, and you don’t need toString()
Uncaught Exceptions
1. [Link]('uncaughtException', callback)
This event listener is used to handle uncaught exceptions—errors that
occur synchronously but are not caught by try...catch.
'uncaughtException': This event is emitted when an exception is thrown
and not handled.
callback (err): A function that receives the Error object (err) and handles
it.
…….
2. Error Logging ([Link])
[Link](...) prints the error message to the console.
The corrected template literal (${err}) ensures the actual error
object/message is properly displayed.
3. Graceful Exit ([Link](1))
[Link](1); forcibly terminates the application with exit code 1, which
typically indicates an error.
Since there is no exception ,we are getting output as usual
I changed the file name here, there is an exception now.
So the exception is logged now …
In [Link], asynchronous read means reading a file without blocking the execution of other code.
How Asynchronous Read works in the previous example:
• ReadFile is called to read [Link].
• Instead of waiting,
• [Link] moves on and logs "Hi".
• Once the file is read, the callback function executes and prints
the file's contents.
• If an error occurs (e.g., file not found), the callback handles it.
• The [Link]('uncaughtException', ...) catches any
unhandled errors globally.
path: The Path module, used to handle file paths across different operating systems.
ensures the correct file path on Windows, Linux, and macOS.
____dirname represents the
current directory.
Joins it with 'Files/[Link]'
[Link] (filePath, data, callback)
Write File
Writes 'Hi guys how are you' into [Link].
If [Link] exists, it overwrites its content.
If [Link] does not exist, it creates a new
file.
(err) => {} If an error occurs, throw err stops execution.
If successful, it logs "Write completed".
Append File Opens [Link] and adds '\n\n Thanks for submitting your data' at the end.
If [Link] does not exist, it creates a new file.
Append should be done with in the call back of write File
Does not overwrite, only adds new content.
We can also use Append to create a new file
Rename a File
This is the new name ([Link]) that [Link] will be renamed to.