What is Node Package Manager (npm)?
Along with 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 based on your operating system. Once downloaded, follow the onscreen instructions to complete the installation process.
For more information, see our guide on installing Node.js on Windows. If you plan to install multiple versions of Node.js on your Linux machine, tools such as NVM can help you manage multiple installations of Node.js.
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.
You can also install multiple packages using a single command by separating the package names with a space like this:
Using installed packages
Once you have installed the packages using the npm install command, it is time for you to start using them. You may notice that a new folder named node_modules and 2 new files, package.json and package-lock.json, were generated automatically. You don’t have 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 consult the documentation for the package you are using on the npm website for the exact syntax.
Uninstalling packages is as easy as installing them. The command to uninstall the packages from your project is:
Just like the install command, you can also uninstall multiple packages with a single command via:
Efficient use of packages
While packages can make your life as a developer easier, they also create a dependency between your project and the packages you use. Therefore, it is recommended that you give it some thought before installing multiple packages.
Rather than dramatically modifying the installed packages to suit your needs, you can also create your own packages and publish them for free to npm. With an appropriate design template, 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