Open In App

npm exec command

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The npm exec command is a powerful feature introduced in npm v7 allowing the users to execute binaries or scripts defined in the node_modules/.bin or those available globally without needing to include them in the PATH. It simplifies running scripts that are part of the project or installed packages.

The npm exec command was introduced to the streamline execution of any scripts provided by the npm packages. Before npm exec we might have used npx for similar tasks but npm exec is a more modern and capable alternative.

Prerequisites

These are the following topics that we are going to discuss:

Syntax:

npm exec [<args>]

Where <args> are the commands or arguments we want to pass to the binary or script we are executing.

Key Components

  • Binaries in node_modules: We don’t need to install a binary globally if it exists locally in node_modules we can run it directly.
  • Script execution: We can execute scripts using the npm commands which helps in keeping the environment consistent.

Key Features of npm exec

  • Project-Specific Binaries: They Run binaries from the local node_modules/.bin folder without needing to alter your environment variables.
  • Compatibility with Global Packages: If a binary isn’t found in the local environment npm exec can also look for the global packages.
  • Arguments Support: We can pass arguments or options to the binaries or scripts we execute using the npm exec.
  • Support for npm Scripts: Easily integrates with the npm scripts for the running commands defined in your package.json.

Common Use Cases

Running Locally Installed Tools:

Many development tools are installed as devDependencies. Instead of running npx eslint src/ we can now use npm exec eslint src/.

Example:

npm exec jest

Running Scripts Defined in Package.json:

We can also execute npm scripts directly using the npm exec.

Example: If your project’s package.json contains a script called build we can run it using:

npm exec run build

Global Packages:

We can run globally installed binaries as well and npm exec will handle whether they are installed locally or globally.

Example:

npm exec -g create-react-app my-app

Options Available with npm exec

Here are some useful options we can use with npm exec:

-c or --call: Used to the specify that the following the arguments should be treated as commands to run in a shell.

npm exec --call "echo 'Hello, World!'"

-g or --global: Ensures the binary is executed globally instead of locally installed.

npm exec -g nodemon

--: Any options after this will be passed directly to the command being executed.

npm exec eslint -- --fix

Examples of Using npm exec

Example 1: Running a Linter

Assume we have eslint installed as the devDependency in the project. To run ESLint and check your code for issues we can simply use:

npm exec eslint src/

Example 2: Running a Command with Arguments

We can pass arguments to the command we are running. For example, running eslint and automatically fixing the issues:

npm exec eslint src/ -- --fix

Conclusion

The npm exec command is a valuable tool for the running scripts and binaries in the Node.js projects. It simplifies executing local binaries, reduces dependency on global installations, and enhances compatibility with npm scripts. With its versatile options and streamlined usage, npm exec is essential for modern JavaScript development workflows.


Next Article

Similar Reads