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
- Where it goes: By default, when you run
npm install <package-name>
, the package is installed in anode_modules
folder within your current working directory. This folder is typically found at the root of your project. - How to use it: The
require()
method on your Node.js application allows you to retrieve locally deployed packages. You may usevar express = require('express');
in your JavaScript file, for instance, after installing Express locally. - 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: - This command downloads Express and its dependencies into
node_modules
and updates yourpackage.json
to reflect this dependency, often looking something like this: - As a project file, the
package.json
file contains dependency information and metadata. Because of its size and created nature, thenode_modules
folder is typically excluded from version control systems like Git, making this especially helpful. Instead, npm will read thepackage.json
file and install all dependencies indicated when other developers or deployment environments executenpm install
(ornpm i
) in the project directory. - Development Dependencies: Use
--save-dev
(or -D) to save packages such as testing tools and linters that are only required during development. When usingnpm install --production
, these are not installed in production environments; instead, they are specified underdevDependencies
inpackage.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 asnvm
. Without usingsudo
, 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.
- Installation: Nodemon is typically installed globally due to its nature as a CLI tool.
- 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
). - Usage: Once installed, you can simply run your application using
nodemon
instead ofnode
: nodemon
will keep an eye on your files andapp.js
will be launched by this command. If you then alter it, for instance:- After saving,
nodemon
will restart itself immediately, allowing you to view the new output without any manual intervention: - You can also integrate
nodemon
into yourpackage.json
‘sscripts
section for easier execution: - From your terminal, you can then just execute
npm start
: - 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 endnodemon
.
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 yourpackage.json
file. For packages that are installed globally, add the -g flag: - npm provides several aliases for
uninstall
, such asremove
,rm
,r
,unlink
, andun
. - 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 usenpm 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:
- MAJOR: When changes are made to the API that are incompatible, MAJOR is incremented. This implies that the current code may fail.
- 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.
- 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.