What Is Node Package Manager (npm)?
Alongside the website, npm also provides a command-line tool that allows developers to install or uninstall these packages.
Visit the official Node.js website to download the appropriate version depending on your operating system. Once downloaded, follow the on-screen instructions to complete the installation process.
For further information, check out our guide on installing Node.js on Windows. If you plan on installing multiple versions of Node.js on your Linux machine, tools like NVM can help you manage multiple Node.js installations.
To verify your installation, open the command prompt on Windows, or the terminal on Linux and macOS, and run the following commands:
If the installation was successful, the terminal will display the installed version of Node.js and npm.
npm install <package_name>
You can also install multiple packages using a single command by separating the package names with a space in this manner:
npm install <package1_name> <package2_name> <package3_name> ... <package_name>
Using the Installed Packages
Once you’ve installed the packages using the npm install command, it’s time for you to start using them. You might notice that a new folder named node_modules and 2 new files, package.json and package-lock.json, have been generated automatically. You don’t need to worry about these files. npm generates them to keep track of your project’s dependencies.
const package = require('package-name');
import package from 'package-name';
You can check out the documentation of the package you’re using from the npm website for the exact syntax.
Uninstalling packages is just as easy as installing them. The command for uninstalling packages from your project is:
npm uninstall <package_name>
Just like the install command, you can also uninstall multiple packages in a single command through:
npm uninstall <package1_name> <package2_name> ... <package_name>
Using Packages Efficiently
While packages can make your life easy as a developer, it also creates a dependency between your project and the packages you’re using. Therefore, it’s recommended you give it some thought before installing multiple packages.
Rather than extensively modifying the installed packages as per your needs, you can also create your own packages and publish them on npm for free. With a proper design pattern, you can create packages for you and your team to use in future projects and make your code reusable.
Image Credit: Ferenc Almasi on Unsplash
About The Author