No More Global Npm Packages (Part 2)

In a previous article that I wrote earlier this year, I talked about eliminating project dependencies that needed to be installed globally, such as Grunt, Gulp, Browserify, WebPack, etc. Of course, I didn’t argue for eliminating these packages, just replacing the -g flag with a --save or --save-dev flag when installing them with npm install and then using npm scripts to execute the binaries. Well, there’s more…

Another Solution

This issue has been rattling around in my head a lot lately, and rattling hard enough to convince me to write 2 more posts (3 more if you count this one) about this subject on other blogs. The first went live September 4th on Sitepoint and is called How to Solve the Global npm Module Dependency Problem and the second was just published on Smashing Magazine as The Issue With Global Node Packages.

npm

Those other 2 articles had something that my Part 1 article didn’t have: an alternative to just using npm scripts. Now, I must say that I still whole-heartedly advocate for using npm scripts if only to help out other people who pick up and use your project or even for yourself if your call to Browserify has several transforms or plugins that you don’t want to type in every time, but sometimes, you just want to type “grunt” or “gulp” or whatever the tool is and make something happen.

Before I reveal the “secret” that a fraction of you may be unaware of, I need to give credit to Gabriel Falkenberg for his comment on my previous post. He’s the one that introduced me to this trick.

So, the trick is that you can add relative paths to your PATH environment variable. For those who may not know, the PATH environment variable is what your console looks at when trying to find the executable file that matches the command you entered. So, for this, specifically, we can add ./node_modules/.bin/ to our PATH, preferrably before the path to the npm globals, but after practically everything else (for security reasons, see the comments by Francesco; you may also want to consider direnv if you’re on a Unix-based machine) so the local binaries will be prioritized over the global npm packages.

Personally, I also added ../node_modules/.bin, and ../../node_modules/.bin to my PATH so that I can be deeper into my project’s file structure and still be able to execute the commands I need. If you’re not sure how to change your PATH, check here if you’re on Windows and here if you’re using a Unix-based machine.

Conclusion

This made things much simpler for me, at least when I’m just getting started with a new tool. Generally, after experimenting for a while, everything will end up in npm scripts anyway. However you do it, I hope this helps you to start saving those modules as dependencies for the sake of everyone else who may work on your project. God bless and Happy Coding!

P.S. You can also just set up your code editor to run your scripts at the press of a button instead of only using the command line directly. I tend to use Sublime Text’s build tools on my projects so that I can just use a couple keystrokes to fire off my most commonly used scripts.

Author: Joe Zimmerman

Author: Joe Zimmerman Joe Zimmerman has been doing web development ever since he found an HTML book on his dad's shelf when he was 12. Since then, JavaScript has grown in popularity and he has become passionate about it. He also loves to teach others though his blog and other popular blogs. When he's not writing code, he's spending time with his wife and children and leading them in God's Word.