NPM tips and tricks to make your life easier

August 14, 2019

If you’ve been using npm and Node for any amount of time, it’s likely you’ve grown tired of typing out install or --save-dev every time you want to add a package. This article will cover some quality of life tips and tricks to make using npm a breeze!

Command

Shortcut

install

i

test

t

Flag

Shortcut

--save-dev

-D

--save

-S

--save-exact

-E

--global

-g

--version

-v

--help

-h

--silentundefined --loglevel silent

-s

--quietundefined --loglevel warn

-q

--loglevel info

-d

--verboseundefined --loglevel verbose

-dd

Install multiple npm packages at once

Most people know this trick, but here it is!

npm i express typescript react

NOTE: --save or -S are not required if you are adding dependencies in npm version 5 or above.

npm i -D express typescript react
// package.json
"devDependencies": {
    "express": "^x.x.x",
    "typescript": "^x.x.x",
    "react": "^x.x.x"
}

You can also install multiple packages that start with the same prefix using the following syntax

npm i react{-router-dom,-bootstrap,-redux}

Install a package from a Github repository

Similarly to packages hosted by NPM, you can install packages directly from GitHub repositories.
NPM understands how a Github repo works and knows what to do if you only specify the username and repo.

npm i https://github.com/microsoft/TypeScript.git
npm i microsoft/TypeScript  

You can even specify a branch name in this manner!

npm i microsoft/TypeScript#add-gdpr-annotations

Open a package’s home page

npm home packagename

The home command will open the homepage specified in a package’s package.json.

if you run npm home typescript it will open the http://www.typescriptlang.org website.

This command can be run without having the package installed locally or globally, so it’s a nice way to verify that the package you’re trying to install is the one you think it is!

Open a package’s GitHub repository

npm repo packagename

The repo command will open the Github repository of the package. This command is similar to the home command and doesn’t require the package to be installed.

Npm init Defaults

Tired of manually entering your information every time you type npm init? You can easily set some defaults using npm config

npm config set init.author.name yourname
npm config set init.author.email youremail
npm config set save-prefix="~"

The ~ prefix tells npm to only upgrade the minor version when you install something, so it’s much less likely to have breaking changes.

Check for outdated dependencies

npm outdated

The outdated command will check with the npm registry and compare package versions - if it finds any that are out-of-date locally it will print them in a list with the current installed version, the ‘wanted’ version (the version specified in your package.json) and the latest version published.

List all installed packages

npm ls --depth 0

To list the globally-installed packages, run the same command with -g flag

npm ls -g --depth 0

Run node modules outside of npm

Occasionally, we find ourselves in need of a tool or library installed via npm in some other application, such as a Dockerfile, CI configuration or a shell script.

The traditional way to run node_modules in this way, is to execute them directly via the node_modules/.bin folder. While this method does still work, there’s now an easier way!

Enter npx – npx is another utility that is now included with npm by default! Using npx is simple:

npx packagename

this will run whatever package you passed it as if it were a script in package.json! Let’s look at another example:

npm i rimraf -D    # install rimraf as a dev dependency
npx rimraf ./build # delete the build directory

Now you can easily run node scripts and tools from your CI system or other CLI scripts.


These are just some of the cool things you can do with npm. While this is not an exhaustive list of everything it can do, I hope it was helpful and makes your life easier in some small way!


Trevor Atlas

👋 Hello,

My name is Trevor Atlas – I'm a Software Developer and Designer based in Washington, DC

For nearly 6 years, I've worked at agencies and startups building functional and intuitive interfaces, flexible and robust services and powerful mobile applications.

When I'm not building user interfaces in React, most of my day-to-day work involves microservices in AWS using Terraform to scaffold infrastructure, Typescript and Go for application logic and Postgres/Redis as a data store. I've also been working on mobile applications with React Native and Expo.