How to use executables from a package installed locally in node_modules?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to use executables from a package installed locally in node_modules?

How to Use Executables from a Package Installed Locally in node_modules?

So you want to use a local version of a module in your node.js app? Maybe you're thinking, "How do I run a command that's installed in my node_modules folder when I'm in my project's main folder?" Well, fear not, dear developer, for I have the solution for you! 🎉

Let's use the example you mentioned, where you installed the coffee-script package in your app:

npm install coffee-script

This command installs coffee-script in the ./node_modules folder, and the coffee command can be found at ./node_modules/.bin/coffee. Now, let's dive into the ways you can use this command conveniently within your project. 💪

Option 1: Using npx

One simple and effective way to use local modules is by utilizing the npx command, which comes with npm by default. With npx, you can run binaries from the local node_modules folder without worrying about the path. Simply prefix the command with npx, like this:

npx coffee

This will execute the local coffee command located inside ./node_modules/.bin no matter where you are in your project's directory structure. 😎

Option 2: Adding Local Binaries to PATH

If you find yourself using the local binaries often and wish to avoid typing npx every time, you can add the ./node_modules/.bin directory to your system's PATH variable. This allows you to run the command as if it were installed globally. Here's how you can do it:

  1. Open your terminal and navigate to your project's root directory.

  2. Run the following command to add the local binaries path to your PATH:

    • Mac/Linux:

      export PATH=$PWD/node_modules/.bin:$PATH
    • Windows (Command Prompt):

      set PATH=%cd%/node_modules/.bin;%PATH%
    • Windows (PowerShell):

      $env:PATH = "$pwd/node_modules/.bin;$env:PATH"

Now, you can simply run the coffee command from anywhere within your project, just like if it were installed globally! 🚀

Option 3: Using scripts in package.json

Another handy way to run local executables is by adding custom scripts to your package.json file. This approach is especially useful when you have multiple local commands to handle for your project. Here's how you can set it up:

  1. Open your project's package.json file.

  2. Find the "scripts" section and add a new script with the desired name. For example:

    "scripts": { "coffee": "coffee" }

Now, you can run the coffee script using npm run:

npm run coffee

By using this approach, you can define multiple scripts with different versions of coffee-script or any other package, ensuring that everyone involved with the project uses the specified version. 📦

Conclusion

Congratulations! You've learned various ways to use executables from a package installed locally in your node_modules folder. You can use npx for quick execution, add the local binaries to your system's PATH for global-like usage, or leverage the scripts section of your package.json for customized command invocation.

Now, go forth and code with confidence! If you have any questions or other cool tricks, feel free to share them in the comments section below. Happy coding! ✨👩‍💻👨‍💻✨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my