Page Content

Tutorials

What is a Global npm? And Global vs. Local npm Modules

What is a Global npm

In the context of npm (Node Package Manager), “Global npm” refers to installing Node.js packages in a system-wide location, making them accessible from any directory on your computer, rather than being confined to a specific project.

Global vs. Local npm Modules

Packages installed with npm can be installed locally or globally, each with a specific function.

Local Installation

  1. Where it goes: By default, when you run npm install <package-name>, the package is installed in a node_modules folder within your current working directory. This folder is typically found at the root of your project.
  2. How to use it: The require() method on your Node.js application allows you to retrieve locally deployed packages. You may use var express = require('express'); in your JavaScript file, for instance, after installing Express locally.
  3. Saving Dependencies: It is essential to save installed packages as dependencies in the package.json file for your project. During installation, this is accomplished by using the --save (or abbreviation -S) flag:
  4. This command downloads Express and its dependencies into node_modules and updates your package.json to reflect this dependency, often looking something like this:
  5. As a project file, the package.json file contains dependency information and metadata. Because of its size and created nature, the node_modules folder is typically excluded from version control systems like Git, making this especially helpful. Instead, npm will read the package.json file and install all dependencies indicated when other developers or deployment environments execute npm install (or npm i) in the project directory.
  6. Development Dependencies: Use --save-dev (or -D) to save packages such as testing tools and linters that are only required during development. When using npm install --production, these are not installed in production environments; instead, they are specified under devDependencies in package.json.

Global Installation

  • Where it goes: Global packages are installed in a single system-wide place, independent of your current directory (for example, C:\Users\YOU\AppData\Roaming pm/node_modules on Windows or /usr/local/lib/node_modules on macOS/Linux). npm root -g can be used to locate this location.
  • How to use it: The main purpose of global packages is to enable you to run command-line interface (CLI) tools from any directory in your terminal. Using require() to import them into your Node.js apps is not an option.
  • Installation: Installing global packages is done by using the --global (or abbreviation -g) flag:
  • Permissions and Best Practices: Because of permission problems and possible security threats, sudo is usually not recommended when performing global npm installs. Instead, think about utilising a Node Version Manager (NVM), such as nvm. Without using sudo, NVM enables you to install Node.js and associated packages in your home directory. Another option is to set up npm to install global modules to a user-writable directory using your ~/.npmrc file.

Nodemon for Development Efficiency

Nodemon is a very helpful command-line tool that, anytime you make changes to your code, restarts your Node.js application. This speeds up development considerably by doing away with the need to manually stop and restart your server after each code change.

  1. Installation: Nodemon is typically installed globally due to its nature as a CLI tool.
  2. It is frequently favoured for project consistency and deployment, while it can also be installed locally as a development dependency (npm install --save-dev nodemon).
  3. Usage: Once installed, you can simply run your application using nodemon instead of node:
  4. nodemon will keep an eye on your files and app.js will be launched by this command. If you then alter it, for instance:
  5. After saving, nodemon will restart itself immediately, allowing you to view the new output without any manual intervention:
  6. You can also integrate nodemon into your package.json‘s scripts section for easier execution:
  7. From your terminal, you can then just execute npm start:
  8. If you want to automatically rerun tests or have complicated startup commands, this is really helpful. In your terminal, you can typically use Ctrl + C to end nodemon.

Managing npm Packages: Uninstalling and Updating

Knowing how to delete and update packages is just as important to effective package management as installing them.

  • Uninstalling Modules: To remove a locally installed package, use npm uninstall <package name>:
  • The module is deleted from your node_modules folder as a result. Use the --save or --save-dev flags, depending on how the package entry was initially saved, to also delete it from your package.json file. For packages that are installed globally, add the -g flag:
  • npm provides several aliases for uninstall, such as remove, rm, r, unlink, and un.
  • Updating Modules: Using npm update, you can update modules to the most recent compatible versions of packages.
  • This command looks for more recent iterations that meet the version restrictions specified in your package.json. You can use npm outdated to see which of the modules you have installed are out-of-date:
  • To upgrade npm to the most recent version, execute:

Semantic Versioning (SemVer)

In order to manage dependencies and comprehend the effects of modifications, npm packages usually adhere to the Semantic Versioning (SemVer) versioning method. MAJOR.MINOR.PATCH is the format for a version number:

  1. MAJOR: When changes are made to the API that are incompatible, MAJOR is incremented. This implies that the current code may fail.
  2. MINOR: When new capability is implemented in a way that is backwards compatible, it is incremented. It should still be possible to use your current code while taking advantage of new capabilities.
  3. PATCH: Applied when bug fixes that are backwards compatible are completed. These upgrades are often safe.

Symbols are used to specify acceptable version ranges for updates in your package.json file:

  • ^ (Caret): E.g., "express": "^4.13.4". This keeps major compatibility intact while enabling npm to update to newer MINOR and PATCH versions.
  • ~ (Tilde): E.g., "eslint": "~6.0.0". Updates to higher PATCH versions inside the designated minor version are the only ones permitted by this.
  • No symbol: E.g., "html": "0.0.10". npm will not update it automatically, therefore just that specific version will be used.

The npm version command can be used to manually increase the version of your package and generate a Git tag:

npm version patch # increments patch version (e.g., 1.2.3 -> 1.2.4)
npm version minor # increments minor version (e.g., 1.2.3 -> 1.3.0)
npm version major # increments major version (e.g., 1.2.3 -> 2.0.0)

Taking care of npm modules and their variations is similar to caring for a garden. Similar to the plants you meticulously grow in designated garden beds, local modules each have a distinct function inside the bed and are recorded on a garden plan (package.json). If you wish to move the entire garden, you simply follow the plan and replant everything, making sure that everything goes in the right place. Like the shovel, watering can, and wheelbarrow that are necessary items in your shed, global modules are also vital.

They may be accessed from any part of your yard, are valuable everywhere, and aren’t specific to any one garden bed. nodemon is your reliable automatic sprinkler system; it maintains your plants hydrated and healthy while immediately resuming the procedure in the event that something changes. Relocating is similar to removing unnecessary plants, while updating is similar to fertilising and pruning to promote robust development. Lastly, SemVer is your gardening bible, letting you know when a change might necessitate a full replanting (major), when it’s safe to introduce new types (minor), or when a patch will suffice.

Index