Lecture Notes Of Day 25:
JavaScript Modules and NPM
Objective:
Learn how to use Node Package
Manager (NPM) and JavaScript modules to manage dependencies and organize your
code.
Introduction
to JavaScript Modules
Modules are reusable pieces of
code in JavaScript that allow you to organize and encapsulate functionality.
With modules, you can split your code into smaller, manageable files and import
only what you need.
Why Use
Modules?
- Code
Reusability: Write once, reuse multiple times.
- Maintainability:
Code is easier to maintain when broken into smaller parts.
- Namespace
Management: Avoid naming conflicts by encapsulating
code.
- Scalability:
Makes it easier to scale projects.
Types of
JavaScript Modules
1. CommonJS
Modules (Used in Node.js):
o
Syntax: require and module.exports.
o
Example:
|
// math.js module.exports = { add:
(a, b) => a + b, subtract:
(a, b) => a - b }; // app.js const math =
require('./math'); console.log(math.add(5,
3)); // Output: 8 |
2. ES Modules (ECMAScript Modules):
o
Syntax: import and export.
o
Example:
|
// math.js export function add(a,
b) { return
a + b; } export function
subtract(a, b) { return
a - b; } // app.js import { add } from
'./math.js'; console.log(add(5, 3));
// Output: 8 |
Introduction
to NPM (Node Package Manager)
What is
NPM?
NPM is a package manager for
JavaScript, primarily used to manage dependencies in Node.js projects. It
allows you to install, share, and manage packages (libraries) efficiently.
Why Use
NPM?
- Easy
Package Management: Install and manage third-party libraries.
- Version
Control: Handle different versions of dependencies.
- Open
Source Community: Access a wide range of open-source packages.
Installing
Node.js and NPM
1. Download
and install Node.js from the official website.
o
NPM is bundled with Node.js.
2. Verify
installation:
o
Check Node.js version: node -v
o
Check NPM version: npm -v
Using NPM
to Manage Packages
1.
Initializing a Project
- Create a new project and initialize it with npm init:
mkdir
my_project
cd
my_project
npm init
-y
- This
command creates a package.json file, which stores metadata about the
project and its dependencies.
2.
Installing Packages
Use the npm install command to
add packages.
- Installing a Specific Package:
npm
install lodash
Adds the lodash package to your
project and saves it in node_modules.
- Installing a Package Globally:
npm
install -g nodemon
Global packages can be used
across projects.
- Using a Package in Your Code:
const _ =
require('lodash');
console.log(_.capitalize('hello
world')); // Output: "Hello world"
3.
Understanding package.json
- Key
sections of package.json:
- dependencies:
Lists the packages required for the project.
"dependencies":
{
"lodash": "^4.17.21"
}
- scripts:
Defines custom commands for your project.
"scripts": {
"start": "node app.js",
"test": "echo \"No test
specified\" && exit 1"
}
Using
Third-Party Modules
Step 1:
Install the Module
Install a third-party library like chalk:
npm
install chalk
Step 2: Use the Module in Code
const chalk = require('chalk');
console.log(chalk.blue('Hello,
world!'));
console.log(chalk.red('Error
occurred!'));
Step 3:
Running the Code
Execute your script:
node
app.js
Best
Practices with NPM and Modules
1.Use .gitignore: Add node_modules
to .gitignore to avoid committing it to version control.
2.Keep
Dependencies Updated: Use npm outdated and npm update.
3.Use
Semantic Versioning: Follow version numbers (MAJOR.MINOR.PATCH)
carefully.
4.Remove
Unused Packages: Use npm uninstall <package-name> to remove
unnecessary dependencies.
Summary
- Modules help
organize JavaScript code into reusable components.
- CommonJS
and ES Modules are two types of JavaScript modules.
- NPM is
a powerful tool for managing packages and dependencies in JavaScript
projects.
- Third-party
libraries can be installed and integrated easily using NPM.
By understanding modules and NPM,
you'll be able to write modular, maintainable code and leverage the extensive
ecosystem of JavaScript libraries.
