The previous NPM quick tips were all about installing packages and we want to enhance it by showing you shortcuts for package installations. NPM integrates multiple time savers for repetitive commands executed from the command line.
To run the commands used within this article, we leverage NPM in version 2.14.0
. You should be save running any 2.x
version of NPM. The commands are also available in NPM 3.
NPM Quick Tips Series Overview
Prerequisites
If you want to execute the commands showed within this article without hesitation, initialize a new Node project with npm init
. Use the new project as a playground and run commands to verify they really work :)
Install Package
We already showed you the increasing number of NPM package installations. There are more than 2 billion downloaded packages within the last 30 days. That means, there is a very high demand on installing packages.
Everybody who has worked with NPM, knows the npm install
command. Submitting this command (without a packagename to install specified) on your command line, NPM searches the current directory for a package.json
file with defined dependencies to install. If there is no package.json
available, NPM won’t do anything. If there is a package.json
available, NPM installs the defined dependencies.
Of course you can install a new package to your local Node project without specifying it within the package.json
file first. You can shortcut the install command by only using i
instead of install
:
npm i lodash
The command above installs the lodash
package into the local node_modules
folder.
Install Multiple Packages With One Command
Within the previous NPM quick tips, we showed you how to install multiple packages within one command. Use the i
shortcut as you would do installing only one package.
npm i lodash hapi thinky when
This will install the packages locally and don’t add them as dependencies to your project. Later within this post, we show you how to install and add packages as dependencies within one command.
Install Package From Github Repository
As with packages hosted on NPM directly, you can install packages directly from GitHub repositories. NPM handles “shortcuts” to GitHub respos in case you only specify the username and repository.
npm i https://github.com/lodash/lodash.git
npm i lodash/lodash
If you rely on a specific branch for a package, you can install it right away by adding #branchname
to the GitHub url:
npm i lodash/lodash#es
Install Package and Save as Dependency
Installing and adding dependencies to your project is a usual step when time goes by and the development advances. If there is already a package you want to use as a dependency for your project, you can easily install and also add it to the dependencies
section within your package.json
. Your peers won’t run into code issues and mental confusion due to the missing package.
The --save
flag shortcuts to -S
:
npm i -S lodash
Of course you can install multiple packages at once and define them as a project dependency simultaneously:
npm i -S lodash hapi thinky when
"dependencies": {
"hapi": "^9.0.3",
"lodash": "^3.10.1",
"thinky": "^2.1.6",
"when": "^3.7.3"
}
Install Package and Save as devDependency
With NPM, you can add development dependencies to your project called devDependencies
. These type of dependencies are only required for development purposes, like testing your code or specify the code coverage. Usually, you would add the --save-dev
flag to an install command.
The --save-dev
flag shortcuts to -D
:
npm i -D mocha istanbul
"devDependencies": {
"istanbul": "^0.3.18",
"mocha": "^2.2.5"
}
Install Global Packages
Besides local packages used within your project, you can install globally available packages on your machine. These are usually packages which expose a command line utility to help you generate a new project seamlessly, use the available functionality directly from command line, or any other capabilities. By default, you need to pass the --global
flag to any install command and of course it has its own shortcut as well.
The --global
flag shortcuts to -g
:
npm i -g mocha
The previous NPM quick tips on installing packages laid the foundation for this post and we advanced the installation process by using shortcuts when installing packages. NPM offers a lot ways to speed up your development flow of times you just need to uncover the smalls thing for tasks you do most often (like installing packages).
Do you have another shortcut which we missed within this article? It would be great if you’re going to share it with the community! Let everyone know in the comments or shout out @futurestud_io.