Open In App

Introduction to packages and modules in npm

Last Updated : 28 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Node Package Manager (npm) serves as a tool for the JavaScript programming language functioning both as a command line utility and package manager. It is the choice for managing dependencies and sharing packages within the NodeJS environment. The public npm registry acts as a centralized platform where developers can upload, discover, and incorporate packages (such, as libraries or modules) into their projects.

Understanding the Public npm Registry

The vast npm library offers an array of open-source packages. It’s like a collection of resources where you can discover ready-made solutions for various web development needs intricate data handling issues, testing utilities, and beyond. Leveraging these packages, from the library significantly speeds up the development process.

The public npm registry goes beyond being a storage place for code; it’s like a lively community hub that encourages sharing knowledge and sparking new ideas. Imagine it as a library filled with ready-made solutions, for all kinds of situations. Developers use the registry to save time and energy by incorporating existing features rather than starting from scratch for typical coding projects.

Packages vs. Modules: The Building Blocks of npm

  • Packages: A package typically consists of a collection of code often comprising JavaScript files along with a package.json file. This particular file serves as the package identification card encompassing details such as its name, version number, dependencies, and scripts for project management. Packages range in scope from utility functions to comprehensive web application frameworks such, as React or Angular.
  • Modules: A module is, like a JavaScript file that offers a particular reusable function. Modules make use of the require() function to bring in code from modules and utilize the exports object to showcase their own functionality. They promote organized and easily maintainable code.

Package Scopes: Organizing Your Packages

Scopes provide a way to namespace your packages. They’re particularly important for larger projects or those developed within organizations.

  • Unscoped Packages: [ For Simplicity ] Living in the global namespace, these packages demand unique names to avoid conflicts. They are often used for smaller-scale or experimental projects.
  • Scoped Packages: [ Fine Grained Control ] Scoped packages are identified by an @ symbol followed by the scope name (e.g., @mygeekorganization/utils). Scoping lets you have multiple packages with the same name as long as they reside in different scopes.

Public vs. Private Packages: Controlling Access

  • Public Packages: Public packages available on the npm registry encourage collaboration in open source projects. Support community driven development. They provide a platform, for sharing code with a wide range of users.
  • Private Packages: Private packages, which require a paid npm subscription are intended for situations where code needs to be kept within an organization or business. These packages are often utilized for software, confidential codebases or packages, in the initial stages of development.

Public Packages: The Heart of Open-Source

Private packages necessitate a paid npm membership. Are designed for code that requires privacy. Companies frequently utilize them for software or, in the initial stages of internal project development.

Scope, Access Level, and Visibility: Explained

Feature

Unscoped
Packages

Scoped
Packages

Scope

Global

Organizational

Access Level

Always Public

Public / Private

Visibility

Public Registry

Public / Private

Naming

package-name

@scope/package-name

Publishing(Default)

npm publish

npm publish

Publishing (public scoped)

Not applicable

npm publish –access public

Features

  • Large ecosystem of packages: npm has a vast ecosystem of open-source packages that cater to a wide range of functionalities.
  • Dependency management: npm helps you manage project dependencies efficiently. It tracks the exact versions of packages required by your project and ensures compatibility.
  • Version control: npm allows you to specify the version of a package you want to install. This helps to ensure that your project works consistently across different environments.
  • Offline usage: npm allows you to cache downloaded packages. This means that you can install packages even when you are not connected to the internet.

Examples:

  • Installing a popular public package (Express Web Framework):
npm install express

express

  • Listing installed packages:
npm list

express2

  • Updating a package:
npm update express

express3

  • Using a package in your code
const express = require('express');

const app = express();

app.get('/', (req, res) => {
res.send('Hello, world!');
});

app.listen(3000, () => {
console.log('Server is listening on port 3000');
});

express4

  • Using a React component ( Public scoped package):
import Button from '@mui/material/Button';
  • Publishing a private scoped package.
    • Create and initialize a package with a package.json file.
    • Set the private field to true in package.json.
    • Run npm publish — the package will be published with private access.

privategeek



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads