Sometimes global and mostly local. Thinks like ts-node, ava, typescript are global and local. Than other thinks like expressjs, body-parser are always local. So when a package has global use I install it global and when not I install it lokal. Installing a package globally allows you to use the code in the package as a set of tools on your local computer. To download and install packages globally, on.
- Node.js Tutorial
- Node.js Useful Resources
- Selected Reading
Node Package Manager (NPM) provides two main functionalities −
- Online repositories for node.js packages/modules which are searchable on search.nodejs.org
- Command line utility to install Node.js packages, do version management and dependency management of Node.js packages.
NPM comes bundled with Node.js installables after v0.6.3 version. To verify the same, open console and type the following command and see the result −
If you are running an old version of NPM then it is quite easy to update it to the latest version. Just use the following command from root −
Installing Modules using NPM
There is a simple syntax to install any Node.js module −
For example, following is the command to install a famous Node.js web framework module called express −
Now you can use this module in your js file as following −
Global vs Local Installation
By default, NPM installs any dependency in the local mode. Here local mode refers to the package installation in node_modules directory lying in the folder where Node application is present. Locally deployed packages are accessible via require() method. For example, when we installed express module, it created node_modules directory in the current directory where it installed the express module.
Alternatively, you can use npm ls command to list down all the locally installed modules.
Globally installed packages/dependencies are stored in system directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but cannot be imported using require() in Node application directly. Now let's try installing the express module using global installation.
This will produce a similar result but the module will be installed globally. Here, the first line shows the module version and the location where it is getting installed.
You can use the following command to check all the modules installed globally −
Using package.json
package.json is present in the root directory of any Node application/module and is used to define the properties of a package. Let's open package.json of express package present in node_modules/express/
Attributes of Package.json
- name − name of the package
- version − version of the package
- description − description of the package
- homepage − homepage of the package
- author − author of the package
- contributors − name of the contributors to the package
- dependencies − list of dependencies. NPM automatically installs all the dependencies mentioned here in the node_module folder of the package.
- repository − repository type and URL of the package
- main − entry point of the package
- keywords − keywords
Uninstalling a Module
Use the following command to uninstall a Node.js module.
Once NPM uninstalls the package, you can verify it by looking at the content of /node_modules/ directory or type the following command −
Updating a Module
Update package.json and change the version of the dependency to be updated and run the following command.
Search a Module
Search a package name using NPM.
Create a Module
Creating a module requires package.json to be generated. Let's generate package.json using NPM, which will generate the basic skeleton of the package.json.
You will need to provide all the required information about your module. You can take help from the above-mentioned package.json file to understand the meanings of various information demanded. Once package.json is generated, use the following command to register yourself with NPM repository site using a valid email address.
It is time now to publish your module −
If everything is fine with your module, then it will be published in the repository and will be accessible to install using NPM like any other Node.js module.
For convenience, many npm based development tools instruct users to install globally. It makes sense if the tool is used to initiate/create a project, but many such modules are also used with an existing projects. These should installed as local (project) dependencies, which have several advantages:
- The version in use is managed alongside other dependencies, keeping the team on the same page. This is especially important for build tools, used to produce a release. Following this practice could improve the reproducibility of builds, which in turn makes deployment and troubleshooting easier and more reliable.
- The number of steps needed for new developers to get up and running is reduced, as all of the necessary tools are installed along with the other project dependencies.
- Multiple versions of a module can be used on the same computer, for example when working on multiple projects which use the tool but may not be upgraded at the same time.
A small barrier to this practice is that local dependencies are not directly executable as they binaries live within the project folder. One fix is to add the projects
node_modules/.bin
folder to PATH, but don’t do this. For one, it would need to be done by every developer needing to use the command.Another option is to add a script to package.json to act as a runner. For example, to make
exp
available as a command in your project, install as a project dependency (using --dev
)and add script entry to act as a runner for your module:The
exp
module will now be installed along with other modules during the installation step, and, thanks to argument forwarding developers can run any subcommand using npm:If using yarn, there is no need to add the script entry. Yarn automatically searches for binaries in
node_modules/.bin
and uses them if present. It also does not need --
for argument forwarding:It may still be useful to add script entries as shortcuts to common commands:
As pointed out by Kevin Kipp, another option is now available, npx, which is installed alongside npm as of npm 5.2.0. Invoking commands with
$ npx
instead of $ npm
will automatically check for and, if present, run the local dependency:Just like yarn, local dependencies are preferred over a global versions of the same modules. Npx, however, goes a step further by automatically installing packages invoked which are not installed either locally or globally. In my opinion this feature will inevitably cause some confusion, surprises, and errors and as such is regrettable.
I’m glad to see this practice of installing everything as a local dependency is finally being embraced and supported by the community. Given the advantages I look forward to this being standard practice. It will take time for it to catch on so if you are a project maintainer with a project which instructs users to install globally, please consider changing the instructions to use a local dependency invoked via npx or yarn.