Use npx to Temporarily Install and Invoke a Package from npm

Elijah Manor
InstructorElijah Manor
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

In this lesson we’ll temporarily install and invoke a node package from npm that we don’t already have installed globally or locally. This is useful for commands that you don’t use regularly and you’d like to stay up-to-date or for tools you want to play around with temporarily.

Instructor: [00:01] I want to create a new React app, but I don't have it installed globally. We could show this by trying it, and sure enough, the command is not found. We could also verify with NPM that it's not listed globally, with the npm ls create-react-app -- global.

[00:18] Nope, it's not there. Finally, we could prove that we don't have it installed in a local Node module with npm ls create-react-app. Again, it's not installed. Instead of installing create-react-app globally, we could use NPX instead to temporarily install the package from NPM and execute it.

[00:40] Let's create a playground React application by typing npx create-react-app playground. At this point, the latest version of the create-react-app is temporarily downloaded. Then it starts to generate the structure of our new React app, and installs all of its necessary dependencies.

[00:59] Now, we could change directories to our new app and type npm start, and it'll kick up a dev server. Voila, our new web app is running on localhost. Using NPX in this way can be very handy if you'd rather not keep programs installed globally, or if you use them so infrequently that you'd rather just use the latest version whenever you need them.

[01:21] Also, it could be handy if you just want to play around with some new tools or ideas before committing to them. For example, let's take a look at devpun. We don't have it installed, but we could start using it with npx devpun.

[01:35] We'll go ahead and pass -t react to filter jokes related to React. Now, we see a joke, even though we don't have devpun installed globally. However, let's continue to play around. Let's pipe the input of devpun to cowsay, with the Vader Cow file.

[01:52] Now, things are getting interesting. Let's continue to play around a bit, and this time, pipe the output to lolcat.js. Wow, colorful. Using NPX, we're able to play around and experiment with various Node modules without even installing them globally. Pretty cool.

egghead
egghead
~ an hour 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