NPM Tricks You Can Use to Impress Your Colleagues


You see a colleague coding, and there are some abbreviations and tricks applied, and somehow, you’re not familiar with it and your brain is all messed up, and this has happened to all of us in the past.

In this article, we will introduce some very useful NPM techniques. There are plenty of tips in articles that cover more than this, so I’ve chosen to focus on the workflows that are most relevant and useful to developers.

Some basic shorthand before we get started

So just to get everyone on track, especially if you’re new, let’s go through the basic shorthand so we don’t miss anything.

The installation package

NPM install PKG Short: NPM I PKG

The installation package is global

NPM I –global PKG NPM I -g PKG

Install packages as dependencies

NPM I — SAVE PKG – NPM I-s PKG

Install packages as development dependencies

NPM I –save-dev PKG NPM I -d PKG

See NPM’s list of abbreviations for the rest

Now let’s do something fun.

1. Initialize a new package

We all know about NPM init, which is the first thing we do when we create a new package.

NPM Init requires a series of questions to be filled out

However, the above problem is very annoying and we will fix it in the meantime, so why don’t we avoid it?

NPM init-y and NPM init-f work well.

npm init -y

2. Run tests

Another command we all use is NPM test, which most of us use every day, possibly several times a day.

npm test

What if I told you to do the same thing with 40% fewer words? We can do it, and it’s a resounding victory.

That’s NPM T.

npm t

3. List the scripts that can be run

When we get a new project, we think about how to get started. We often ask ourselves these things: How do I run it? What scripts are available?

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

package.json

We can certainly do better, so we can run NPM run and get a list of all the available scripts.

npm run

The remaining options are to install NTL (NPM I-g NTL) and then run NTL in the project file directory, which makes running scripts very convenient.

ntl

4. List the installed packages

Similar to the scripts available, sometimes we ask ourselves: what dependency packages do we have in the project?

We could open the package.json file again to check, but we already know we can do better.

Welcome NPM LS –depth 0.

npm ls –depth 0

NPM ls -g –depth 0

npm ls -g –depth 0

5. Run the locally installed executable file

We installed the package in the project, it comes with an executable file, but it only runs through NPM script, do you want to know why or how to take it down?

First of all, to understand why — when we run a command in Terminal, what actually happens is that it lists all the paths in the PATH environment variable to look for executables with the same name. Packages installed locally only register their executables locally, so they are not listed in the PATH environment variable and cannot be found.

At this point you may ask, how does this work when we run the executable through the NPM script? Good question! This is because NPM uses some sleight-of hand and adds extra folders to the PATH, /node_modules/.bin.

You can run through the NPM run env | grep “$PATH to see it, can run NPM run env to see all of the available environment variables, NPM will add some interesting things.

Node_modules /.bin, if you know, is exactly where the local installation package puts its executables.

In the project directory, let’s run./node_modules/.bin/mocha.

./node_modules/.bin/mocha

Easy, right? Whenever you want to run locally installed executables, run./node_modules/.bin/.

6. Find your bag online

You might see the entry to the repository in the package.json file and wonder what it does?

To answer this question, simply run the NPM repo to open and view it in your browser.

Similarly, the NPM home command corresponds to a homepage entry.

If you want to open a package page at nPMjs.com, there’s a great shorthand for that too, NPM Docs.

7. Run the script before and after other scripts

You may be familiar with scripts such as pretests, which allow you to write code before running test scripts.

You may be pleasantly surprised to find that you can have pre and Post scripts for each script, including your own.

pre and post script

This can be very useful in projects that use NPM as a build tool and require a lot of scripting.

8. Check the package version

If you have a package that uses semver (Semantic Versioning specification) to control versioning, you need to do a versioning check before releasing a new version.

One way is to open the package.json file to manually change the version, but we’d prefer not to do that.

An easier way is to run NPM Version with major, minor, or Patch.

npm version

That’s all for now.

I hope you can learn something new and find a useful technique for your daily work, have a better understanding of NPM and have some new ideas that you can apply to your work.

To impress your colleagues better, learn new things often and become more professional.

If you know of other practical tips, please share them in the comments