SyntaxStudy
Sign Up
Node.js Beginner 8 min read

npm Scripts and npx

npm scripts let you define shortcuts for common commands in package.json. Run them with npm run <script-name>. Built-in scripts like start and test can be run without the run keyword.

npx executes a package without installing it globally, useful for one-time tools like project scaffolding.

Example
// package.json — scripts section
{
    "scripts": {
        "start":    "node src/index.js",
        "dev":      "nodemon src/index.js --watch src",
        "build":    "tsc -p tsconfig.json",
        "test":     "jest --coverage",
        "lint":     "eslint src/**/*.js",
        "format":   "prettier --write src/**/*.js",
        "db:seed":  "node scripts/seed.js",
        "postinstall": "echo 'Dependencies installed!'"
    }
}

# Run scripts:
npm start          # node src/index.js
npm run dev        # nodemon ...
npm test           # jest --coverage
npm run lint

# npx — run without global install:
npx create-react-app my-app
npx cowsay "Hello npm!"

# Check for outdated packages:
npm outdated

# Update a package:
npm update express

# See details about a package:
npm info lodash version