LinuxCommandLibrary

npm-run-script

Execute commands defined in package.json scripts

TLDR

View documentation for the original command

$ tldr npm run
copy

SYNOPSIS

npm run-script [-- ]

PARAMETERS


    The name of the script to execute, as defined in the `scripts` section of `package.json`.

--
    Arguments to pass to the script. Arguments after `--` are passed directly to the executed script.

DESCRIPTION

The `npm run-script` command executes scripts defined in the `scripts` section of your `package.json` file. This provides a convenient way to automate common tasks like building, testing, and deploying your project. Instead of directly executing commands in the terminal, you define aliases in your `package.json` file, making your workflow more organized and reproducible.

`npm run-script` handles environment variables and ensures that the `node_modules/.bin` directory is added to the `PATH`, allowing you to execute locally installed dependencies without specifying their full path. This makes it easier to use tools like `eslint`, `webpack`, or `mocha` within your scripts. If no script name is provided, it will list all available scripts. It provides more benefits than running scripts manually, like platform independent execution since the user doesn't have to know which shell to use.

By using `npm run-script` we standardize project management and promote automation.

CAVEATS

If no script name is provided, `npm run-script` will list available scripts. Running `npm run` is an alias for `npm run-script`. If a script name matches an npm command, it will not execute. Script names should not include spaces or special characters to avoid parsing issues.

USAGE EXAMPLES

Example 1: Running a build script
```npm run build```
Example 2: Passing arguments to a test script
```npm run test -- --grep='specific test'```
Example 3: Listing all available scripts
```npm run```

PACKAGE.JSON SCRIPTS SECTION

The `scripts` section in package.json is a JSON object where keys are script names and values are the commands to execute. For example:
```json "scripts": { "start": "node index.js", "test": "mocha", "build": "webpack" } ```

HISTORY

The `npm run-script` command has been a core feature of npm since its early versions. It was designed to provide a standardized and cross-platform way to define and execute common project tasks, replacing ad-hoc shell scripts and custom build tools. It evolved alongside npm and the Node.js ecosystem, becoming an essential tool for modern JavaScript development workflows. The use of the `scripts` section in `package.json` file has become a common standard in modern projects.

SEE ALSO

npm(1), package.json(5)

Copied to clipboard