Introduction to NPM
NPM ("Node Package Manager") is the default package manager for JavaScript's runtime Node.js. The packages (libraries, tools, frameworks, etc.) required for your project are installed and managed using npm. Additionally, it aids in managing your project's dependencies (the libraries required for it to run).
Npm consists of three components:
- The npm registry: This is a database of open-source packages that are available for use in your projects. You can use npm to search for and install packages from the registry.
- The npm command-line interface (CLI): This is the tool that you use to interact with the npm registry and manage your projects. You can use the npm CLI to install packages, update packages, run scripts, and perform other tasks.
- The npm website: This is the website (https://www.npmjs.com/) where you can browse and discover packages that are available in the npm registry, as well as find documentation and other resources related to npm.
Npm is known as the world’s largest open-source packages registry. You can use npm to install packages from the npm registry, Developers all over the world use npm to publish and share their source code. To install a package, you can use the npm install command followed by the name of the package you want to install. For example, to install the popular JavaScript library called "lodash", you can run the following command:
npm install lodash
Once a package is installed, you can use it in your project by importing it into your JavaScript code.
In addition to installing and managing packages, npm also provides other useful features such as the ability to publish packages to the npm registry, create and manage projects, and run scripts.
Overall, npm is a powerful and essential tool for any JavaScript developer, and it plays a key role in the modern JavaScript ecosystem.
package.json
Every npm project has a file called package.json located in the root directory and is used by the npm command-line interface (CLI) to manage the project's dependencies and scripts. The package.json file is a JSON (JavaScript Object Notation) file that is used to define the metadata and configuration for a Node.js project. The package.json file contains information about the project, such as the project's name, version, description, and the dependencies it requires to function. It also includes scripts that can be run to perform common tasks, such as building and testing the project.
To create the package.json file, you go to the root directory of the project and execute the following command:
npm init
Here is an example of a basic package.json file:
{ "name": "my-project", "version": "1.0.0", "description": "A simple Node.js project", "main": "index.js", "scripts": { "test": "jest" }, "dependencies": { "lodash": "^4.17.20" }, "devDependencies": { "jest": "^26.0.1" } }
The dependencies field lists the packages that are required for the project to run, while the devDependencies field lists packages that are only needed for development purposes (e.g. testing frameworks, build tools, etc.).
You can use the npm CLI to manage the dependencies and scripts in your package.json file. For example, you can use the "npm install" command to install the dependencies listed in the package.json file, or you can use the "npm run" command to run a script defined in the scripts field.
Install a new package
To install a new package, you use the following "npm install" command. For example, if you want to install the express package, you can run the following command:
npm install
Once the installation is completed, you will see a new directory called "/node_modules" created under the root of the project. All the new modules that you install will be placed in this directory.
If you open the package.json file in the root of the project, you will also find that the dependencies section is updated
Install a package as a development dependency
To install a package as a development dependency, you can use the "--save-dev" flag with the npm install command. For example, to install the jest testing framework as a development dependency, you can run the following command:
npm install--save-dev
This will install the package and add it to the devDependencies field in the package.json file.
Install a package globally on your system
To install a package globally on your system, you can use the "-g" flag with the npm install command. For example, to install the package globally, you can run the following command:
npm install-g
Note that installing a package globally may require you to have administrative privileges on your system, depending on your setup. If you don't have administrative privileges, you may need to use sudo to install the package globally.