You watch a colleague code, and you see shorthand and techniques being used in ways you’re not familiar with, and you’re being looked down on, and that happens in all of our lifetimes.

In this article, I will introduce some useful NPM techniques, and there are many more that can’t be covered here, but we will focus on those that are relevant and useful to our daily work and development.

Basic shorthand

In order to get everyone on the same page about NPM, especially our newcomers, let’s get up to speed on basic shorthand and make sure we don’t miss any details.

Installation Package

General: NPM install PKG, shorthand: NPM I PKG

Global installation package:

NPM I –global PKG, shorthand: NPM I -g PKG.

Install packages and include them in dependencies:

General: NPM I –save PKG, shorthand: NPM I-s PKG.

Install packages and include them in development dependencies:

NPM I –save-dev PKG, shorthand: NPM i-d PKG.

Other NPM shorthand read the NPM blog shorthand sheet.

Now for the fun.

1. Initialize a new package

We all know NPM init, which is used to create a new package.

However, all problems are anonymous and we will fix them anyway, so how do we avoid them?

Use NPM init -y and NPM init -f to go back to initialization!

2. Run the test command

Another command is NPM test, which we might use daily or several times a day.

What if I told you that you could do this with less than 40% characters? We use it a lot. It should.

Fortunately, we can use NPM T, and it will do the job

3. Enumerate available scripts

We’re starting a new project and we want to know how to get started. We often ask ourselves questions like: How do we run it? What scripts are available?

One way is to open the package.json file and check the scripts section.

We can do better, of course, by simply running NPM run to get a list of all available scripts.

Another option is to install NTL (NPM i-g NTL) and run NTL in the project folder. It can also run scripts, which is also very nice.

4. Enumerate installed packages

Similar to the scripts available, sometimes we ask ourselves what dependencies we have in our project.

We can open the package.json file again and check, but we’re doing better.

Use NPM ls –depth 0.

To enumerate global installation packages, use NPM ls -g –depth 0

5. Run the locally installed executable file

We installed a package in our project and it was an executable, but we could only run it through a new script. You want to know why, or how to overcome it?

First, let’s understand why — when we execute a command in a terminal, what actually happens is that it looks for an executable with the same name in all the paths listed in the PATH environment variable. That’s how they can magically execute from anywhere. Locally installed packages register their executables locally, so they are not in our PATH PATH and will not be found.

When we run these executable files through a new script, how does it work? You may ask? Good question! This is because when running this way, NPM does a little trickery and adds an extra folder to PATH, /node_modules/.bin. You can see it running NPM run env | grep “$PATH.” You can also run just NPM run env to see all the environment variables available, NPM adds some more interesting things. /node_modules/.bin, in case you didn’t know, is exactly where your locally installed packages put the executable.

We can run./node_modules/.bin/mocha in our project to see what happens.

Simple, right? Just run./node_modules/.bin/ whenever you want to run locally installed executables.

6. Find the bag you want online

You might see the repository entry in the package.json file and ask “What good is this?”

To answer this question, just run the NPM repo and look it up in your browser.

Similarly, the NPM home command goes to the home page.

If you want to open your package’s home page, nPMjs.com, that’s a nice shorthand, NPM Docs.

7. Run the script before and after other scripts

You may be familiar with scripts such as Pretest, which allow you to define code to run before the test script.

You might be surprised to learn that you can configure either a with or after script for each script, including your own custom script!

For projects where you use NPM as your build tool, there are many scripts that you need to summarize and use yourself.

8. Change the package version

You develop an installation package, and you use Semver as a version management tool to modify a new version.

In this way, you need to manually modify the package.json file, which is not recommended.

An easier way is to run NPM version through Major, Minor or Patch


That’s all for this video.

I hope you learn new things, find use in these tips, and use them in your daily workflow. The best thing is that you gain a new understanding of NPM and use it better in your work.

It’s great to impress your colleagues, but it’s better to keep learning new things and becoming more professional!

If you know of any more helpful tips, please share them in the comments!