Introduction to NPM | Lecture 2
Here are
concise notes on NPM (Node Package Manager):
Introduction to NPM
What is NPM?
- NPM (Node Package Manager) is a package manager for
JavaScript.
- It comes bundled with Node.js.
- Used to install, share,
manage, and update JavaScript libraries and tools.
Core Features of NPM:
- Package Installation:
- Allows developers to
install and use pre-built libraries or tools.
- Packages are hosted on the
NPM registry.
- Dependency Management:
- Automatically tracks and
manages libraries that a project depends on.
- Version Control:
- Supports installing
specific versions of packages and updating them to newer versions if
needed.
- Custom Scripts:
- Enables running
custom-defined commands using npm run.
- Publishing:
- Developers can publish
their own packages to the NPM registry.
Common Commands:
- Installing Packages:
- Locally: Installs in the project
directory.
o npm install <package-name>
- Globally: Installs for use across
projects.
o npm install -g <package-name>
- Uninstalling Packages:
3. npm uninstall <package-name>
- Updating Packages:
5. npm update <package-name>
- Viewing Installed Packages:
- Local:
o npm list
- Global:
o npm list -g
- Initializing a Project:
- Creates a package.json file for the project.
o npm init
- Running Scripts:
- Scripts defined in package.json can be run using:
o npm run <script-name>
Key Files:
- package.json:
- A configuration file that
stores metadata about the project, such as:
- Project
name, version, description
- Dependencies
- Scripts
for tasks
- package-lock.json:
- A file that locks the exact
versions of installed dependencies to ensure consistency.
Types of Dependencies:
- Regular Dependencies: Installed for runtime use.
2. npm install <package-name>
- Development Dependencies: Installed for development
purposes only.
4. npm install <package-name> --save-dev
Advantages of NPM:
- Huge Ecosystem:
- Over a million open-source
packages available.
- Ease of Use:
- Simple commands for package
management.
- Scalability:
- Efficient handling of
large-scale projects with complex dependencies.
Example Workflow:
- Initialize a Project:
2. npm init
- Install a Library (e.g.,
Express):
4. npm install express
- Use the Installed Library:
6. const express = require('express');
7. const app = express();
8.
9. app.get('/', (req, res) => {
10.
res.send('Hello, NPM!');
11.});
12.
13.app.listen(3000, () => console.log('Server
running on port 3000'));
Let me
know if you'd like further clarification or examples!
Comments
Post a Comment