0
0
NodejsHow-ToBeginner · 3 min read

How to Use npx in Node.js: Quick Guide and Examples

Use npx in Node.js to run Node packages without installing them globally by typing npx <package-name> in your terminal. It downloads and runs the package temporarily, making it easy to try tools or run scripts quickly.
📐

Syntax

The basic syntax of npx is:

  • npx <package-name>: Runs the package directly.
  • npx <package-name>@<version>: Runs a specific version of the package.
  • npx --package <package-name> <command>: Runs a command from a package without installing it globally.
bash
npx <package-name> [options]
💻

Example

This example shows how to use npx to run the create-react-app package to create a new React project without installing it globally.

bash
npx create-react-app my-app
Output
Creating a new React app in /your/path/my-app. Installing packages. This might take a couple of minutes. ... Success! Created my-app at /your/path/my-app Inside that directory, you can run several commands: npm start Starts the development server. npm run build Bundles the app into static files for production. npm test Starts the test runner. npm run eject Removes this tool and copies build dependencies, configuration files and scripts into the app directory. We suggest that you begin by typing: cd my-app npm start Happy hacking!
⚠️

Common Pitfalls

Common mistakes when using npx include:

  • Trying to run packages that are not published on npm or misspelled package names.
  • Expecting npx to install packages permanently; it runs them temporarily.
  • Not having Node.js and npm installed, which are required for npx to work.

Example of a wrong command and the correct way:

bash
npx create-react-app
# Wrong: Missing project name

npx create-react-app my-app
# Correct: Includes project name
📊

Quick Reference

Here is a quick cheat sheet for npx usage:

CommandDescription
npx Run a package without installing it globally
npx @Run a specific version of a package
npx --package Run a command from a package
npx -p Shortcut for --package option
npx --no-install Run command only if package is installed locally

Key Takeaways

Use npx to run Node.js packages without global installation.
Always specify the package name and required arguments when using npx.
npx downloads and runs packages temporarily, so no permanent install happens.
Make sure Node.js and npm are installed before using npx.
Use npx <package>@<version> to run a specific package version.