npm vs npx — What’s the Difference?

npm-vs-npx-whats-the-difference-twitter

If you’ve ever used Node.js, you must have used npm for sure.

npm (node package manager) is the dependency/package manager you get out of the box when you install Node.js.
It provides a way for developers to have a global and local installation of packages. Sometimes you’d want to take a look at a specific package and try out some commands. But you cannot do that without installing the dependencies in your local node_modules folder.
That’s where npx comes in.

In this article, we’re going to have a look at the differences between the two and how to get the best from both.
First, let’s understand what npm actually is and what we can do with it.

npm the package manager

npm is a couple of things.
First and foremost, it is an online repository for the publishing of open-source Node.js projects.
Second, it is a CLI tool that aids you in installation, version and dependency management of those packages. There are hundreds of thousands of Node.js libraries and applications on npm and many more are added every day.

npm by itself doesn’t run any package. If you want to run a package using npm, you must specify that package in your package.json file.

When executables are installed via npm packages,  npm creates links to them:
– local installs have links created at ./node_modules/.bin/ directory
– global installs have links created from global bin/ directory (for example: /usr/local/bin on Linux or at %AppData%/npm on Windows)

To execute a package with npm you would either have to type the local path

$ ./node_modules/.bin/your-package

 or you can run a locally installed package by adding it into your package.json file in the scripts section

{
  "name": "your-application",
  "version": "1.0.0",
  "scripts": {
    "your-package": "your-package"
  }
}

then you can run the script using npm run

$ npm run your-package

You can see that running a package with plain npm requires quite a bit of ceremony.

Fortunately, this is where npx comes in handy.

npx the package runner

Since version npm 5.2.0 npx is pre-bundled with npm. So it’s pretty much a standard nowadays.
npx is also a CLI tool whose purpose is to make it easy to install and manage dependencies hosted in npm registry.
It’s now very easy to run any sort of Node.js based executable that you would normally install via npm.

You can run the following command to see if it is already installed for your current npm version:

$ which npx

if not, you can install it like this

$ npm install -g npx

Once you make sure you have it installed, let’s see a few of the use cases that make npx extremely useful:

Run a locally installed package easily

If you wish to execute a locally installed package, all you need to do is type:

$ npx your-package

npx will check whether <command> or <package> exists in $PATH, or in the local project binaries and executes it.

Execute packages that are not previously installed

Another major advantage is the ability to execute a package that wasn’t previously installed.
Sometimes you just want to use some CLI tools but you don’t want to install them globally just to test them out. This means you can save some disk space and simply run them only when you need them. This also means your global variables will be less polluted.

Run code directly from GitHub

execute-gist-scripts-with-npx

This one’s pretty rad.
You can use npx to run any GitHub gists and repositories. Let’s focus on executing a GitHub gist because it’s easier to create one.

The most basic script consists of the main JS file and a package.json.
After you’ve set up the files, all you have to do is run the npx with the link to that gist as shown in the image above.
Here you can find the code that I used for this example.

Make sure you read carefully any script before you execute it to avoid serious problems that can occur due to malicious code.

Test different package versions

npx makes it extremely easy to test different versions of a Node.js package or module. To test this awesome feature, we’re going to locally install create-react-app package and test out an upcoming version.

This will list some dist tags near the end of the output. Dist tags provide aliases for version numbers which makes it so much easier to type.

$ npm v create-react-app

create-react-app-dist-tags

Let’s use npx to try out the next dist tag of create-react-app which will create the app inside a sandbox directory.

$ npx [email protected] sandbox

npx will temporarily install the next version of create-react-app, and then it’ll execute to scaffold the app and install its dependencies.
Once installed, we can navigate to the app

$ cd sandbox

and then start it with

$ npm start

create-react-app-npx-next-version
It will automatically open the React app in your default browser window.
Now we have an app that runs on the next version of create-react-app package!

Conclusion

npx helps us avoid versioning, dependency issues and installing unnecessary packages that we just want to try out. It also provides a clear and easy way of executing packages, commands, modules and even GitHub gists and repositories.

If you haven’t used npx before, now it is a good time to start!

Let me know how would you make use of npx and what do you think about this article through comments below, or on Twitter at @pelu_carol.

If you find this article helpful, please share it with others and subscribe to the blog to support me and receive a bi-monthly-ish e-mail notification on my latest articles.

Follow me

Carol Pelu

Neutron Star at Neutron Dev
Second most tallest chap in the neighbourhood. Sometimes my sense of humor causes abdominal pain. I am not yet famous for something.
Carol Pelu
Follow me

You may also like...