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
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.
Similar Reads
npm explore Command npm explore command is used to temporarily enter references to packages installed on the machine, so you can execute commands in the environment, it's especially useful for debugging or checking the status of files and dependencies in a package.What Is the npm explore Command?The npm explore command
2 min read
npm edit command The command that can simplify the editing process of the project's files is npm edit. This command is useful for opening a file editor directly from the command line which allows users to make changes to the projectâs files effortlessly. This article will provide a comprehensive overview of the npm
3 min read
NPM Docs Command The npm docs command is a convenient way to access the official documentation of any npm package directly from the command line. It opens the documentation page of the specified package in your default web browser, making it easier for developers to quickly find and explore package details, APIs, an
4 min read
npm bin Command The npm bin command is a lesser-known but incredibly useful command in the Node.js ecosystem. It provides information about the location where npm installs globally executable binaries or locally installed binaries for the current project. In this article, weâll explore the details of the npm bin co
4 min read
CLI Commands in NPM NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integra
4 min read