blog

  • Home
  • blog
  • 10 Tips and Tricks That Will Make You an npm Ninja

10 Tips and Tricks That Will Make You an npm Ninja

While there is much excitement about Facebook’s new Yarn project, the continuing success of Node.js owes much to its original package manager, npm.

A few simple npm commands is all it takes to initialize a folder (npm init), download packages (npm install) and create tests (npm test) and custom scripts (npm run) for use in your project. Few delve further but there are several npm tips and tricks which can revolutionize your daily development tasks.

Note: if you need a primer on npm, check out our beginners guide. If you’re confused as to the difference between npm and Yarn, see our post: Yarn vs npm: Everything You Need to Know.

1. Getting Help!

The npm online help and CLI Command documentation is excellent but switching to and from your browser is not always convenient. A quick reminder of all options is available from the command line:

npm help

Help for specific npm commands can also be displayed:

npm help <command>

for example, npm help install.

Or you can view a quick command parameter reference:

npm <command> -h

2. npm Command Autocomplete

npm offers command auto-completion for systems using bash (including Bash for Windows 10):

npm completion >> ~/.bashrc

or Z shell:

npm completion >> ~/.zshrc

Reload the shell configuration file, e.g.

source ~/.bashrc

Now type npm ins and hit TAB and install will appear. You need never waste time typing in full ever again!

3. Fixing Global Module Permissions

Linux-like systems can throw permission errors when you attempt to install global packages. You can prepend sudo to any npm command but that’s a dangerous option. A better solution is to change npm’s default directory to one you own:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

Add the following line to ~/.bashrc or ~/.zshrc as appropriate using your text editor of choice:

export PATH="$HOME/.npm-global/bin:$PATH"

Reload the shell configuration file (source ~/.bashrc) then reinstall npm itself to the new user-owned location:

npm install -g npm

This will also update npm to the latest version.

Continue reading %10 Tips and Tricks That Will Make You an npm Ninja%

LEAVE A REPLY