CLI node modules

Share this video with your friends

Social Share Links

Send Tweet
Published 10 years ago
Updated 5 years ago

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.

Man 1: [00:01] 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.

[00:18] 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.

[00:41] 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.

[01:02] 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.

[01:34] 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.

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

egghead
egghead
~ 47 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today