Use npx to Temporarily Install and Invoke a Package from npm

Elijah Manor
InstructorElijah Manor

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 2 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.