Professional Documents
Culture Documents
NPM Vs NPX Whats The Difference
NPM Vs NPX Whats The Difference
freecodecamp.org/news/npm-vs-npx-whats-the-difference
Carol-Theodor Pelu
If you’ve ever used Node.js, then 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 install
packages both globally and locally.
Sometimes you might 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.
In this article, we’re going to have a look at the differences between the npm and npx and
learn how to get the best from both.
First, let’s understand what npm actually is and what we can do with it.
1/7
Watch Video At: https://youtu.be/fSHWc8RTJug
Second, it is a CLI tool that aids you install those packages and manage their versions and
dependencies. 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 packages. 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:
To execute a package with npm you either have to type the local path, like this:
$ ./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, like this:
2/7
{
"name": "your-application",
"version": "1.0.0",
"scripts": {
"your-package": "your-package"
}
}
You can see that running a package with plain npm requires quite a bit of ceremony.
npx is also a CLI tool whose purpose is to make it easy to install and manage dependencies
hosted in the 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
Once you make sure you have it installed, let’s see a few of the use cases that make npx
extremely helpful.
$ npx your-package
npx will check whether <command> or <package> exists in $PATH, or in the local project
binaries, and if so it will execute it.
3/7
Execute packages that are not previously installed
Another major advantage is the ability to execute a package that wasn’t previously installed.
This is awesome because 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.
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.
4/7
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.
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 the 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
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 will temporarily install the next version of create-react-app, and then it’ll execute to
scaffold the app and install its dependencies.
$ cd sandbox
$ npm start
5/7
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!
This is how the index page of your React app should look like.
Conclusion
npx helps us avoid versioning, dependency issues and installing unnecessary packages that
we just want to try out.
6/7
It also provides a clear and easy way of executing packages, commands, modules and even
GitHub gists and repositories.
Carol-Theodor Pelu
Self-taught Web Developer. I write geeky stuff @ https://NeutronDev.com
If you read this far, thank the author to show them you care.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than
40,000 people get jobs as developers. Get started
7/7