NPM stands for Node Package Manager. It is a tool used to manage and share reusable packages of code for Node.js. It is a command-line utility that installs, updates and uninstalls packages.
NPM is installed automatically with Node.js. It is used to install packages globally or locally in a particular folder.
Installing packages with npm
Notes on installing packages with npm:
- During the project development process, there may be a need to add specific packages.
- To install packages, navigate to the project directory and run the command
npm install <package_name>
. - This command will add the package as a dependency in the project's package.json file.
- It is essential to have a correctly structured package.json file for the installation to work.
- If the package.json file is missing or incorrectly structured, the installation will fail.
- npm provides a built-in command,
npm init
, which can be used to create the package.json file. -
npm init
allows you to initialize a new project and generate a package.json file with the required structure. - With the generated package.json file, you can then proceed to install additional packages using
npm install
. Remember to always maintain a properly structured package.json file and use the appropriate npm commands to install and manage packages within your project.
To install a package globally, you can use the following command:
npm install -g {package name}
To install a package locally, you can use the following command:
npm install {package name}
Package.json
NPM also allows you to manage dependencies for a project. You can create a package.json
file which contains the list of dependencies for your project. To create this file, you can run the following command:
npm init
This will prompt you to enter the details of your project such as the name, version, description, and author. Once you have entered the details, it will create a package.json
file for you.
To install all the dependencies listed in your package.json
file, you can run the following command:
npm install
Notes on package.json and npm:
- The package.json file is used to specify the required packages for a JavaScript application.
- It lists out each package's name and version number.
- When the command
npm install
is run in a directory with a package.json file, npm reads the dependencies listed in package.json. - It downloads the packages from npmjs.com, the hosting platform for npm packages.
- The installation process also involves resolving dependencies for each package.
- Each package may have its own package.json file with its specific dependencies, forming a dependency tree.
- npm ensures that all dependencies, including nested ones, are fetched and installed.
- In a local environment, running
npm install
creates a folder called "node_modules." - The node_modules folder contains all the downloaded packages necessary for the application. Remember to regularly update your package.json file to manage and track the required packages and their versions accurately.
Installing, updating, removing packages
NPM also allows you to update and uninstall packages. To update a package, you can use the following command:
npm update {package name}
To uninstall a package, you can use the following command:
npm uninstall {package name}
In conclusion, NPM is a powerful tool that allows you to manage and share packages of code for Node.js. It simplifies the process of installing, updating and uninstalling packages and managing dependencies.
Publishing packages with npm
Another important feature of NPM is that it allows you to publish your own packages to the NPM registry, which can be used by other developers. To publish a package, you need to first create a package.json
file, which includes the details of the package such as name, version, and dependencies. You can then use the following command to publish the package:
npm publish
NPM also provides a way to manage multiple versions of the same package, which is useful when you need to maintain compatibility with different versions of Node.js or other dependencies. You can specify the version of a package when you install it by using the @
symbol followed by the version number, like this:
npm install {package name}@{version}
Top comments (2)
Hey this is a great article!
If you add tags to the post then more users are likely to see it.
Totally forgot to add tags, did so now thanks for the reminder. Glad you enjoy the article