Skip to content
tezvyn:
Menu

npm Scripts: Your Project's Command-Line Shortcuts

Source: docs.npmjs.comEasyHow cards are made

npm Scripts: Your Project's Command-Line Shortcuts

Think of npm scripts as aliases for your project's common command-line tasks, stored in package.json. Use them to run build tools, start servers, or execute tests. The main footgun is forgetting npm run for custom scripts like lint or build.

Why it exists

Projects often require running complex, multi-step commands for tasks like testing, building, and deploying. Remembering these commands, ensuring all team members use the same flags, and managing them becomes difficult. npm scripts solve this by providing a central, version-controlled place to define and run these tasks.

The mental model

npm scripts are like a project-specific command-line alias system. You define a named script in your package.json file, which maps a short name (like dev) to a longer shell command (like nodemon --watch src src/index.js). Anyone with the project can then run npm run dev without needing to know the underlying command.

How it works

The scripts field in package.json is a JSON object where keys are the script names and values are the commands to execute. When you run npm run <script-name>, npm temporarily adds your project's node_modules/.bin directory to the system's PATH. This lets you execute command-line tools from your project's dependencies (like jest or webpack) directly by name, without needing global installation.

When to use it

Use scripts to automate any repetitive task in your development workflow. This is ideal for starting servers, running linters and formatters, executing test suites, compiling code (like TypeScript or Sass), and creating production builds. It's the standard way to manage a JavaScript project's lifecycle.

When not to use it

Avoid putting long, complex shell logic directly inside package.json. For multi-step processes with conditional logic, it's better to write a separate shell script (.sh) or Node.js script (.js) and have the npm script simply execute that file. This keeps your package.json clean and your logic more maintainable.

One canonical example

A common package.json might have these scripts: "scripts": { "start": "node server.js", "dev": "nodemon server.js", "test": "jest", "lint": "eslint ." }. Here, npm start runs the server. npm run dev runs it with nodemon for auto-restarts. npm test runs the Jest test runner. npm run lint checks the code style. Note that start and test are special and don't require the run keyword.

Interview question

What is the primary mechanism that allows npm scripts to execute project-specific tools (like jest or webpack) without requiring them to be installed globally?

  • a.npm temporarily adds the project's node_modules/.bin directory to the system's PATH during script execution.Correct
  • b.npm scripts have a built-in resolver that can locate executables within any node_modules folder.
  • c.npm scripts automatically detect and install any missing global dependencies required by the script.
  • d.The package.json file explicitly lists all executable paths for tools used in scripts.
Why?

The card states that npm temporarily adds the project's node_modules/.bin directory to the system's PATH, allowing local executables to be run by name. Option C is incorrect because npm does not automatically install global dependencies for scripts.

Just read this? Test yourself on what you have been reading.

Read the original → docs.npmjs.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on npm — each one lists the topics its interview covers.

See open roles