Executing Local NPM Packages: A Simple Guide

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.

NPX stands for Node Package eXecute. All it is is a package runner for NPM (Node Package Manager). Without even needing to install it, developers may use any Javascript package that is listed in the NPM registry. NPM versions 5.2.0 and higher come with an automated installation of NPX. It makes it easy to use CLI (Command Line Interface) tools and other executables located on the registry, in the same way that npm makes it simple to install and manage dependencies hosted on the registry.

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”.

Output:

Image of using npx command

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.

{
  "scripts": {
    "dev": "figlet"
  }
}
Image of package.json file

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

Output:

You should see the “figlet” output, creating ASCII art from the text “Anurag” in the terminal.

Image of running npm run dev

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.

ls node_modules/.bin/

Step 2) Run the desired local binary directly from the “node_modules/.bin/” directory. In this example, we will use the “figlet” package.

.\node_modules\.bin\figlet Anurag

Output:

Image of running local binaries

Conclusion

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.

References

Anurag Pandey
Anurag Pandey
Articles: 16