I.NPM Quick Tips
#2 Use Shortcuts to Install Packages
#3 Show Installed and Outdated Packages
Update All Node.js Dependencies to Their Latest Version
# How to list all versions of an npm module?
npm view react versions --json
=> This is pretty much the same but won’t list alpha or beta releases
npm show webpack@* version
# How to list npm user-installed packages?
npm list
npm list --json
=> List only packages names without deep level
npm list --depth=0
# To see list of all packages that are installed. (List installed packages)
https://docs.npmjs.com/cli/v6/commands/npm-ls
npm ls --parseable
npm ls
npm-view
how-to-list-all-versions-of-an-npm-module
List Package in NPM Terminal
npm i windows-ls -g
npm list graphql
npm view graphql
git config --list
4. Check npm package updates :
npm install -g npm-check-updates
:/> ncu
Run ncu -u to upgrade package.json
I.NPM Quick Tips
NPM is the package manager of choice when working with Node.js. We don’t need to emphasize the importance of knowing your tools and of course, this holds true for NPM as well. This post will show you tips and tricks using NPM to show installed packages for your local repository or packages installed globally on your system.
To run the commands used within this article, we leverage NPM in version 3.3.4. You should be save running any 2.x version of NPM and have the same output as the examples below.
Installed Packages
The first thing coming to your mind when reading of using NPM to show the installed packages: why shouldn’t I just open the package.json and look up the dependencies section? You know, that is for sure a valid thought and of course you’ll find what you’re looking for. The thing is, using the command line may be faster or you don’t want to open you IDE just to look if a given package is a dependency within your project.
You can leverage NPM’s functionality and list the installed packages for your project. The following command will print a list of installed packages and their dependencies:
npm ls
# or
npm list
If you’re just interested in the list of installed packages which you directly depend on, use the –depth option and pass –depth=0.
$ npm ls --depth=0
futurestudio-homepage@1.0.0 /Users/marcuspoehls/Dev/FutureStudio/homepage
├── bcrypt-as-promised@1.0.1
…
└── when@3.7.3
There is also an NPM command to print additional package information while listing the installed modules:
npm ll
# or
npm la
Both commands npm ll and npm la will print the same information which looks like this:
$ npm ll
futurestudio-homepage@2.0.1
│ /Users/marcuspoehls/Dev/FutureStudio/homepage
│ Future Studio homepage based on hapi, handlebars and Twitter bootstrap
│ https://futurestud.io/giturlto/homepage.git
│ https://futurestud.io
├── bcrypt-as-promised@1.0.1
│ A promisified bcrypt
│ git+ssh://git@github.com/iceddev/bcrypt-as-promised.git
│ https://github.com/iceddev/bcrypt-as-promised
…
└── when@3.7.3
A lightweight Promises/A+ and when() implementation, plus other async goodies.
git+https://github.com/cujojs/when.git
http://cujojs.com
You’ll receive the general project information including the current version number, the project’s location on your local system, project’s description and url. All that information is read from your package.json file, no magic attached. Further, the dependency tree will list the installed packages also including the version number, package description, git url and homepage.
Useful Options – –json: of type boolean, default value is false and possible values are true|false – –long: of type boolean, default value is false and possible values are true|false – –depth: of type int and possible value is any positive number
Note: There are also options to show only the production dependencies (–prod=true) or only the development dependencies (–dev=true). To be honest, we never restrict the list of installed packages to a given environment, since we’re interested in the list of all packages. You can find further information about the –dev and –prod options on NPM’s documentation page for ls.
Upgrade NPM Package
npm i npm-check-updates -u
Outdated Packages
Maintaining your app and dependencies is essential for security and getting newly available features from packages you depend on. NPM offers the outdated command to print a list of packages which are out of date.
npm outdated
The list of outdated packages includes the currently installed version, the wanted version defined within your package.json file and the latest stable version of the module. The output also shows a location which indicates the project name for which you requested the list of outdated packages.
$ npm outdated
Package Current Wanted Latest Location
boom 2.7.2 2.7.2 2.9.0 futurestudio-homepage
handlebars 3.0.3 3.0.3 4.0.3 futurestudio-homepage
hapi 8.5.3 8.5.3 10.0.1 futurestudio-homepage
As you can the in the example above, we currently have three outdated packages for our homepage project. Ok ok, you’ve made your point 😀 … Maintenance required!
You can also use the outdated command for your globally installed packages:
npm outdated -g
You see, there are also three packages outdated on my machine.
$ npm outdated -g
Package Current Wanted Latest Location
npm-check-updates 2.2.0 2.2.3 2.2.3 lib
pm2 0.14.7 0.15.5 0.15.5 lib
supervisor 0.7.1 0.8.0 0.8.0 lib
Options
There are options for the outdated packages as well. The example below uses the –long option to additionally show the package type for the outdated packages. The package type is either dependencies or devDependencies.
$ npm outdated --long=true
Package Current Wanted Latest Location Package Type
boom 2.7.2 2.7.2 2.9.0 futurestudio-homepage dependencies
handlebars 3.0.3 3.0.3 4.0.3 futurestudio-homepage dependencies
hapi 8.5.3 8.5.3 10.0.1 futurestudio-homepage dependencies
Useful Options
–long: of type boolean, default value is false and possible values are true|false
–depth: of type int and possible value is any positive number
Note: There is also the option to print the list of outdated packages as JSON (–json=true). The complete overview of options is available on NPM’s documentation page for outdated.
II
#2 Use Shortcuts to Install Packages
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.
III
#3 Show Installed and Outdated Packages
Installed Packages
The first thing coming to your mind when reading of using NPM to show the installed packages: why shouldn’t I just open the package.json and look up the dependencies section? You know, that is for sure a valid thought and of course you’ll find what you’re looking for. The thing is, using the command line may be faster or you don’t want to open you IDE just to look if a given package is a dependency within your project.
You can leverage NPM’s functionality and list the installed packages for your project. The following command will print a list of installed packages and their dependencies:
npm ls
# or
npm list
If you’re just interested in the list of installed packages which you directly depend on, use the –depth option and pass –depth=0.
$ npm ls --depth=0
futurestudio-homepage@1.0.0 /Users/marcuspoehls/Dev/FutureStudio/homepage
├── bcrypt-as-promised@1.0.1
…
└── when@3.7.3
There is also an NPM command to print additional package information while listing the installed modules:
npm ll
# or
npm la
Both commands npm ll and npm la will print the same information which looks like this:
$ npm ll
futurestudio-homepage@2.0.1
│ /Users/marcuspoehls/Dev/FutureStudio/homepage
│ Future Studio homepage based on hapi, handlebars and Twitter bootstrap
│ https://futurestud.io/giturlto/homepage.git
│ https://futurestud.io
├── bcrypt-as-promised@1.0.1
│ A promisified bcrypt
│ git+ssh://git@github.com/iceddev/bcrypt-as-promised.git
│ https://github.com/iceddev/bcrypt-as-promised
…
└── when@3.7.3
A lightweight Promises/A+ and when() implementation, plus other async goodies.
git+https://github.com/cujojs/when.git
http://cujojs.com
You’ll receive the general project information including the current version number, the project’s location on your local system, project’s description and url. All that information is read from your package.json file, no magic attached. Further, the dependency tree will list the installed packages also including the version number, package description, git url and homepage.
Useful Options – –json: of type boolean, default value is false and possible values are true|false – –long: of type boolean, default value is false and possible values are true|false – –depth: of type int and possible value is any positive number
Note: There are also options to show only the production dependencies (–prod=true) or only the development dependencies (–dev=true). To be honest, we never restrict the list of installed packages to a given environment, since we’re interested in the list of all packages. You can find further information about the –dev and –prod options on NPM’s documentation page for ls.
Outdated Packages
Maintaining your app and dependencies is essential for security and getting newly available features from packages you depend on. NPM offers the outdated command to print a list of packages which are out of date.
npm outdated
The list of outdated packages includes the currently installed version, the wanted version defined within your package.json file and the latest stable version of the module. The output also shows a location which indicates the project name for which you requested the list of outdated packages.
$ npm outdated
Package Current Wanted Latest Location
boom 2.7.2 2.7.2 2.9.0 futurestudio-homepage
handlebars 3.0.3 3.0.3 4.0.3 futurestudio-homepage
hapi 8.5.3 8.5.3 10.0.1 futurestudio-homepage
As you can the in the example above, we currently have three outdated packages for our homepage project. Ok ok, you’ve made your point 😀 … Maintenance required!
You can also use the outdated command for your globally installed packages:
npm outdated -g
You see, there are also three packages outdated on my machine.
$ npm outdated -g
Package Current Wanted Latest Location
npm-check-updates 2.2.0 2.2.3 2.2.3 lib
pm2 0.14.7 0.15.5 0.15.5 lib
supervisor 0.7.1 0.8.0 0.8.0 lib
Options
There are options for the outdated packages as well. The example below uses the –long option to additionally show the package type for the outdated packages. The package type is either dependencies or devDependencies.
$ npm outdated --long=true
Package Current Wanted Latest Location Package Type
boom 2.7.2 2.7.2 2.9.0 futurestudio-homepage dependencies
handlebars 3.0.3 3.0.3 4.0.3 futurestudio-homepage dependencies
hapi 8.5.3 8.5.3 10.0.1 futurestudio-homepage dependencies
Useful Options
–long: of type boolean, default value is false and possible values are true|false
–depth: of type int and possible value is any positive number
Note: There is also the option to print the list of outdated packages as JSON (–json=true). The complete overview of options is available on NPM’s documentation page for outdated.
IV
Update All Node.js Dependencies to Their Latest Version
Show Outdated NPM Packages
The NPM CLI shows outdated packages in your project. Run npm outdated in a project directory to show a list of outdated packages.
$ npm outdated
Package Current Wanted Latest Location
ava 1.0.0-rc.2 1.0.1 1.0.1 boost
aws-sdk 2.374.0 2.374.0 2.382.0 boost
listr 0.14.2 0.14.3 0.14.3 boost
mongoose 5.3.16 5.3.16 5.4.0 boost
nodemailer-postmark-transport 1.4.0 1.4.1 2.0.0 boost
sinon 7.2.0 7.2.2 7.2.2 boost
vision 5.4.3 5.4.4 5.4.4 boost
Review the installed version of a module, the wanted version satisfying the package’s version range and the latest version.
The downside: there’s no command in NPM’s CLI to update all packages to their latest version. You need to manually go through your package.json file and bump all versions. But wait, there’s help!
NPM-Check-Updates
The npm-check-updates package is a convenient helper providing useful features for dependency upgrades. Install the command line tool globally on your machine with this command:
npm install -g npm-check-updates
As soon as the package installation finished, you’ll have the ncu command available on your computer. Run it in your project’s folder to check the project’s dependencies for updates:
$ ncu
aws-sdk ~2.374.0 → ~2.382.0
mongoose ~5.3.16 → ~5.4.0
nodemailer-postmark-transport ~1.4.0 → ~2.0.0
The following dependencies are satisfied by their declared version range,
but the installed versions are behind. You can install the latest versions
without modifying your package file by using npm update. If you want to
update the dependencies in your package file anyway, run ncu -a.
vision ~5.4.3 → ~5.4.4
ava ~1.0.0-rc.2 → ~1.0.1
listr ~0.14.2 → ~0.14.3
sinon ~7.2.0 → ~7.2.2
Notice that the list of outdated packages is different from NPM’s overview. The first list includes all packages that can’t be installed due to the defined version range in your package.json file.
You can see the leading tilde symbol ~ for each version which means only patch updates satisfy the range. Each item in the list is either a minor or major update.
The second list shows all packages that would install with the next run of npm update. Their version range still satisfies the latest release and installing updates does not need a version bump.
Update All Dependencies
npm-check-updates comes with handy flags to conveniently update your packages. The following command will update all your dependencies to their latest version:
ncu -ua
# the same as "ncu --upgradeAll"
The -u flag will update all packages that didn’t satisfy the version range (major and minor versions in this example). The -a flag is responsible for bumping the versions that would still satisfy the range.
That’s it! All your dependencies are up to date and require the latest version.