A complete understanding of the node_modules directory – a vital repository containing the dependencies required for application development, is crucial for navigating the complex environment of Node.js applications.
A few of these dependencies provide executable commands that might be very useful tools to improve the capabilities of your project. We will explore the methods of using executables from locally installed packages in the node_modules directory in this article.
Note: Since I’ll be utilizing the “figlet” package for the examples in this article, all of the examples assume that the package is already present in the project. You may use the following command to install the package:
npm i figlet figlet-cli
Here we have installed it locally. The “figlet” package installed globally should be executable by running the command “figlet” straight from the console. Assuming you wish to use the package that is installed in your project instead of the global one, I recommend you read, to get an in-depth understanding of Local and Global installation of npm packages.
Executing Local NPM Packages Using NPX
NPX: An npm package runner — that helps to execute packages without installing them explicitly.
There may have been times when you just wanted to execute a command once. That may have been the case since, similar to create-react-app. Installing these scripts globally first and then using them in other situations is very redundant. These are the circumstances where NPX’s actual abilities are revealed. Here’s an example of utilizing the entertaining command “figlet”.
Using npx to initiate the execution of a package. If is not already in your $PATH, npx will install the package from the npm registry and invoke it. npx will not maintain the packages in the globals.
Note: Consider using npx for tasks that are one-time or infrequent to keep things simple.
Executing Local NPM Packages Using NPM Scripts
NPM scripts are a powerful feature provided by npm (Node Package Manager) that allows developers to define and execute various scripts or commands within a Node.js project. These scripts are configured in the “scripts” field of the project’s package.json file. In this section we will see that npm scripts for the execution of “figlet” commands provide developers with a powerful and customizable approach.
Let’s explore how to use npm scripts to execute “figlet” a package that generates ASCII art text:
Step 1) Open our “package.json” and locate the “scripts” field inside your file.
Step 2) Inside the “scripts” field, add a script named “dev” that executes the “figlet” command.
Step 3) Run the “dev” Script. For this, you can use the “npm run” command followed by the script name. The above program can be executed using the below command.
npm run dev Anurag
You should see the “figlet” output, creating ASCII art from the text “Anurag” in the terminal.
Note: Use the scripts in package.json to handle difficult or often-used activities for improved maintainability and structure.
Also Read – A Beginner’s Guide to Node Package Manager
Executing Local NPM Packages By Running Local Binaries
Your development environment becomes simpler when you run local binaries since you are running command-line tools straight from locally installed packages.
Running local binaries directly provides a simple and understandable method for working with project-specific binaries.
Step 1) Check that the local binaries are present in the “node_modules/.bin/” directory. List the contents of the directory to ensure the binaries are there.
Step 2) Run the desired local binary directly from the “node_modules/.bin/” directory. In this example, we will use the “figlet” package.
Finally, this tutorial has covered three efficient ways to launch local packages in Node.js: using npx, adding scripts to package.json, and running local binaries directly. With regard to meeting specific demands within the development workflow, each technique has advantages of its own. The decision between these approaches ultimately comes down to the particular requirements of the project, how often commands are used, and how clear-cut the project setup has to be.