NPM, or Node Package Manager, is the default package manager for Node.js, and it plays a crucial role in managing dependencies, devDependencies, authors, application entry-point, packages, and scripts in Node.js projects.
The NPM registry is a central repository for storing npm packages and managing and maintaining versioning history. with the help of `npm cli` commands-line tools, we can download those packages in our current project. it will get downloaded in the `node_modules` folder
Here's an introduction to NPM along with some of its use cases:
NPM allows developers to easily install external packages and libraries into their Node.js projects.
These packages can include various functionalities, utilities, and modules that can be used to enhance the capabilities of a Node.js application.
npm install package-name or yarn add package-name
NPM helps manage project dependencies by creating a "package.json" file that lists all the required packages along with their versions.
This makes it easy for developers to share and reproduce the project environment.
npm init
NPM allows us to define and run custom scripts in our project. These scripts can automate various tasks, such as running tests, create project build, or start a project in a different environment.
// package.json "scripts": { "start": "node server.js", "test": "jest" } // in terminal type // npm run start // npm run test // above commands, will refer to the attributes in scripts object > npm run start or npm run test
NPM allows the installation of packages globally, making them available as command-line tools.
This is useful for tools that are intended to be used across different projects.
npm install -g package-name
NPM follows Semantic Versioning (Semver), making it easy to specify and manage version constraints for project dependencies.
This helps prevent compatibility issues when updating packages.
// package.json "dependencies": { "package-name": "^2.4.10" }