This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

CLI node modules

2:07 Node.js lesson by

Many node modules provide a command line interface. In this lesson, we'll look at how to use them, as well as how to avoid npm install -g for "global" tools.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Many node modules provide a command line interface. In this lesson, we'll look at how to use them, as well as how to avoid npm install -g for "global" tools.

Some node modules provide a command line utility that can be used from the shell. Mocha is one of the things that does that. The way that it does that is in Mocha's package.json it lists, in this case, two files. Generally there's just going to be one.

In this .bin identifier it lists out the files that it would like for MPM to make available from the command line. What MPM does in response is it creates this .bin folder within your node modules folder. It's regularly hidden from view. It then creates shortcuts to those files that were specified by Mocha.

What you can then do to run that directly from the shell is to simply use that path to the local node modules folder and then .bin and then the name of the file. In this case we're just going to ask for the version. You can see there that the locally installed version of Mocha is 1.18.2.

Now, a lot of tutorials and instructions that you'll see recommend to install things globally simply so that you can run them from the command line but that's actually entirely unnecessary. You can use the method that we just did to access the local path. You can use the scripts fields of your package.json which we'll cover later. Or the even easier way is to simply add that local path that we just used to your path environment variable.

If you were to set to run this command. I'm not going to because I already have it. But if you were to add the ./node_modules./bin to your local path you can then simply use this file directly. I can just say Mocha-version and it's going to be that locally installed version.

I can even say which Mocha and it will tell me that I'm point at that locally installed version.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?