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:
Open your terminal and navigate to your project's root directory.
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:
Open your project's
package.json
file.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.
