Using npm scripts and npx

Wrap common commands in npm scripts and use npx to run local or remote packages on demand.

🎯 Introduction

In this article, you will learn two useful tools: npm scripts and npx. They let you turn complex commands into a simple npm run xxx and run NPM package commands without installing them globally.


🧱 What are npm scripts?

npm scripts are commands defined in package.json. They wrap long commands used during development in short, memorable names.

For example:

package.json
{
    "scripts": {
        "dev": "vite",
        "build": "vite build",
        "preview": "vite preview"
    }
}

To run them, use:

npm run dev
npm run build
npm run preview
Defining commands in scripts makes them easier to remember. It also lets teammates get started by reading package.json, without memorizing each tool's commands.

✨ Common script patterns

Run multiple commands

When a script needs multiple steps, you can use && or &:

package.json
{
    "scripts": {
        "build-and-preview": "npm run build && npm run preview",
        "lint-and-format": "eslint . && prettier --write ."
    }
}
  • &&: run the next command only if the previous command succeeds.
  • &: run commands concurrently.

Call other scripts

Within scripts, use npm run script-name to call another script:

package.json
{
    "scripts": {
        "clean": "rm -rf dist",
        "build": "npm run clean && vite build"
    }
}

Special scripts

Two scripts can run without run:

npm start    # Runs scripts.start
npm test     # Runs scripts.test
npm start and npm test are conventional entry points. start usually starts the project, while test runs tests.

🧰 Environment variables and lifecycle scripts

Environment variables

Scripts can access fields from package.json through process.env.npm_package_xxx. For example:

package.json
{
    "name": "my-app",
    "version": "1.0.0",
    "scripts": {
        "info": "node -e \"console.log(process.env.npm_package_name)\""
    }
}

Running npm run info prints my-app.

Lifecycle scripts

NPM automatically runs certain scripts at specific points. For example:

package.json
{
    "scripts": {
        "postinstall": "nuxt prepare"
    }
}

postinstall runs automatically after npm install finishes, often to prepare the project for development.

Lifecycle scripts are convenient, but avoid putting expensive work in them, as it slows down every dependency installation.

🚀 What is npx?

npx is a command-line tool included with NPM since version 5.2.0. It runs NPM package commands on demand without a global installation.

Run a remote package on demand

For example, to quickly create a Vite project:

npx create-vite@latest

This downloads and runs create-vite without adding it to your global installation, keeping your global environment uncluttered.

Run locally installed packages

Many tools install executables in node_modules/.bin, which your shell does not find by default. Use npx to run them directly:

npx eslint .
npx prettier --write .
When a package is installed in the project's node_modules, npx can find and run its command. This lets you use tools such as ESLint and Prettier without installing them globally.

⚖️ npm scripts versus npx

ScenarioRecommended approachReason
Everyday project commandsnpm run xxxDefined in package.json and shared with the team
Run a package command oncenpx package-nameNo global installation needed
Run a locally installed toolnpx tool-nameFinds commands in node_modules/.bin automatically
Start or test a projectnpm start / npm testConventional, short, and memorable

❓ Questions and answers

Q1: How does npm run build differ from running vite build directly?

npm run build calls the script in package.json, using the project's installed tool version. Running vite build directly may use a global version; mismatched versions can cause errors.

Q2: How does npx differ from a global installation?

A global installation remains available system-wide. npx downloads a package on demand or uses a local copy, without adding a permanent global installation.

Q3: How do && and & differ in scripts?

&& runs commands in sequence, proceeding only after success. & allows commands to run concurrently.

Q4: When does postinstall run?

It runs automatically after npm install finishes installing dependencies, often to generate type declarations or prepare the environment.


🧪 Practice

Complete these tasks in your npm-demo project:

  1. Add the following scripts to package.json:
package.json
{
    "scripts": {
        "hello": "node -e \"console.log('Hello from scripts')\"",
        "build": "echo 'Building...' && echo 'Build complete'"
    }
}
  1. Run npm run hello and npm run build, and inspect the output.
  2. Try running cowsay, a playful command-line tool, once with npx:
# Enter your command here

🎉 You now know how to use npm scripts and npx! Next, we will learn to publish an NPM package and switch registry mirrors to speed up downloads.