Created
August 6, 2018 07:10
-
-
Save wertyoo/82c538f812dc8589edf433789af16719 to your computer and use it in GitHub Desktop.
NPM - How to target locally installed bin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The problem with putting | |
./node_modules/.bin | |
into your PATH is that it only works when your current working directory is the root of your project directory structure (i.e. the location of node_modules) | |
Independent of what your working directory is, you can get the path of locally installed binaries with | |
npm bin | |
To execute a locally installed coffee binary independent of where you are in the project directory hierarchy you can use this bash construct | |
PATH=$(npm bin):$PATH coffee | |
I aliased this to npm-exec | |
alias npm-exec='PATH=$(npm bin):$PATH' | |
So, now I can | |
npm-exec coffee | |
to run the correct copy of coffee no matter of where I am | |
$ pwd | |
/Users/regular/project1 | |
$ npm-exec which coffee | |
/Users/regular/project1/node_modules/.bin/coffee | |
$ cd lib/ | |
$ npm-exec which coffee | |
/Users/regular/project1/node_modules/.bin/coffee | |
$ cd ~/project2 | |
$ npm-exec which coffee | |
/Users/regular/project2/node_modules/.bin/coffee | |
you can even go one step further and alias coffee="npm-exec coffee" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment