Node Package Manager
Updated on Oct 5, 2023
The Node Package Manager (NPM) is Node.js’ way of handling modules (packages). Through it, developers can install almost any kind of module they need for their project. For instance, unlike the WordPress plugin manager, NPM is not a graphical interface. Instead, it uses CLI, but the functionality is similar: download and install modules to enhance the functionality of your application.
NPM is included in the Node.js installation by default, and there is no need to install it separately. Once you have Node.js installed, NPM will be accessible via the command line with its own set of commands. Many commands can perform tasks like installing Node.js modules, checking the integrity of your environments, compiling a package, running a security audit, and so on. Commands can often take flags as well, which are modifiers to the command itself, instructing it to perform the given task but with specific parameters. They are added after the command and any values it may have. We will mention a few commands and parameters, the most fundamental ones, to give you a few examples. Still, we strongly recommend reviewing the documentation about the commands we linked earlier.
- npm install - This command is one of the most used ones, and it will install any modules (packages or libraries) specified within the package.json file. That file contains all the modules that a particular application needs to function. The command looks through all available repositories and installs the packages if found. Alternatively, you can add the name of a module after the command to install the specific module. Just make sure you have the correct name. The modules will be installed in the node_modules directory of the application;
- --no-save - Use this flag if you don’t want the installed module to be saved to the application’s dependencies;
- npm uninstall - The exact opposite of the above command. It will uninstall the specified package as long as the correct name is placed after the command.
- --save - With this flag, the module will be uninstalled and removed from the application’s dependencies;
- --no-save - If this flag is added, then the module will be uninstalled and removed from the dependencies;
- npm ls - This is a vital command that will show you all the modules installed on the current application and their dependencies. The naming format is name@version, and any dependencies will be shown directly under their module. If a GIT URL is associated with a module, it will be displayed in parentheses after the version. Keep in mind that invalid or missing packages will also be shown, which can be a good indication of why an application might not be functioning correctly;
- npm start - As the name suggests, this command starts your application. Of course, this can only be done if there is a start property in the scripts section of the package.json file;
- npm stop - Stops your application if a stop script is provided in the package.json file;
- npm cache clean - This command will clean NPM’s cache to reclaim the space it took, but at the moment, it can only be executed with a specific flag;
- --force - This flag must be added to the clean command from above. Nonetheless, some applications can get very big, and their cache can bloat up in size, so clearing it can be beneficial;
- npm view - Use this command to view the data associated with a particular package. The command will show you all information associated with the package, and the version it shows will always be the latest unless specified otherwise;
- npm view name@version dependencies - This variation of the above command shows you all the dependencies of the specified package and version.
As you can see, there are some fundamental tasks that NPM can perform, but there are many more things it can do that can get more complicated. The folks behind the NPM CLI have extensive documentation about all the commands and flags on their website. The commands from above should be sufficient to get you started with Node.js, though, and you can expand your knowledge from there.