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
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read