Execute Packages With npx And npm
The npm package executor (npx) comes in handy to run one-off commands from packages that I want to use but don't need installed, polluting the global namespace.
[#npx-vs-npm]The difference between [.inline-code]npx[.inline-code] and [.inline-code]npm[.inline-code][#npx-vs-npm]
The node package manager ([.inline-code]npm[.inline-code]) allows developers to manage, install, and update packages from the npm registry. On the other hand, [.inline-code]npx[.inline-code] makes it easier for developers to run these packages without having to install them explicitly. These two go hand in hand and [.inline-code]npx[.inline-code] comes pre-bundled with [.inline-code]npm[.inline-code] since version 5.2.0.
With [.inline-code]npx[.inline-code], you do not need to think about if a package has already been installed before running it. If the package does not exist, [.inline-code]npx[.inline-code] will install the package to a folder in the npm cache.
The key difference between the two is that [.inline-code]npm[.inline-code] requires multiple steps to run packages. You will need to use [.inline-code]npm[.inline-code] to first install the package, and then perform extra steps to execute.
Let's run through an example using the [.inline-code]create-react-app[.inline-code] package, which is a popular package for quickly creating and setting up react projects. The package will create a directory that contains an initial project structure and install the required dependencies to get started with React.
[#executing-a-package-with-npx]Executing a package using [.inline-code]npx[.inline-code][#executing-a-package-with-npx]
The following [.inline-code]npx[.inline-code] command will execute the [.inline-code]create-react-app[.inline-code] package, which will in turn create a new directory called [.inline-code]my-app[.inline-code] in the current directory.
With this approach, no global installation of the library is needed and the latest version of the package is installed. Assuming that you won't be using [.inline-code]create-react-app[.inline-code] often, using [.inline-code]npx[.inline-code] is a great way to save on disk space and not worry about conflicting versions of the same package.
Using a specific version of a package
If you want to use a specific version of a package instead of the latest, you can specify the version number right after the package name.
For example, if you want to use version [.inline-code]3.0.0[.inline-code] of the [.inline-code]create-react-app[.inline-code] package, you can append it like so:
The official npm docs provide many more examples of using the npx flags and options.
[#executing-a-package-with-npm]Executing a package using [.inline-code]npm[.inline-code][#executing-a-package-with-npm]
[#executing-a-global-package]Using a globally installed package[#executing-a-global-package]
To execute a global package with [.inline-code]npm[.inline-code], you can start by installing it globally using the [.inline-code]npm install -g[.inline-code] command:
And directly execute it using its name:
This method is similar to using [.inline-code]npx[.inline-code] with some caveats, as you are now polluting the global space with the package and after some time you will be using an outdated version that may have breaking changes.
[#executing-a-local-package]Using a locally installed package[#executing-a-local-package]
To execute a local package, you can start by installing it in your project using the [.inline-code]npm install[.inline-code] command:
Define a script in the [.inline-code]scripts[.inline-code] section in the [.inline-code]package.json[.inline-code] file, which is a way to store commonly used commands for a project, such as test or build commands:
And execute this script using the [.inline-code]npm run[.inline-code] command:
[#using-npx-with-github-gists]Using [.inline-code]npx[.inline-code] with Github gists[#using-npx-with-github-gists]
Another cool thing about [.inline-code]npx[.inline-code] is being able to use it to run Github gists and repositories.
You can try this out by creating your own gist using the following steps:
- Create a new gist on github
- Create a package.json file
- Create an index.js file
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/6508c02410d610bdd63b3d87_yJlH0ipYnvol4HL7BvE5QRloCCEysOuNcKqIJbN9jRx-87YlQiLMiXi0pG3pga6UPJDbaWGkrypRMp1tJHPV8UQ0JBb8VMuTzHFA_OeXT_4FjFCNn5HtLaRhwDNLFFZfb1XCNqHqMrKlXY8xENGInAo.gif)
And now you're ready to run this with [.inline-code]npx[.inline-code] using the URL of the gist:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/6508c052fc5fe9adb243c37e_k66anWp05y2twbZ6h_zexd3YgYpy4NvjgbS8sSLKbtfMDujxisvypeAIzyj9dYSADZzRVBttXI0bncfGyskynda7qeaHK_rGUVBQtMcpwDvdcFkTXWH_5oJgO0pAgFWLl-WKdSh02BmdobOjIqJUmJc.gif)