Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Should I add ~/node_modules/.bin to my $PATH #957

Closed
timoxley opened this issue May 22, 2011 · 8 comments
Closed

Should I add ~/node_modules/.bin to my $PATH #957

timoxley opened this issue May 22, 2011 · 8 comments
Labels

Comments

@timoxley
Copy link
Contributor

local as default is really frustrating, just wondering if adding ~/node_modules/.bin to my path would fix me having to use -g EVERY time i install a module that comes with a commandline tool?

@isaacs
Copy link
Contributor

isaacs commented May 22, 2011

You can add whatever folder to your path that you like :)

Of course, then it'll only work if you do a local install in ~. If you're in some other folder, it won't put the bin in ~/node_modules. But, you could put node_modules/.bin in your PATH, and then you'll be able to use the bins installed in that folder when you're in that folder.

@airtonix
Copy link

I know this is an old ticket, but it shows up on google for "npm which PATH", so I thought I'd leave my work around here.

This only works on sh, bash, zsh (pretty much any shell that uses ~/.bashrc, ~/.profile etc):

https://gist.github.com/airtonix/9925531

disclaimer: it's ugly, but it makes using node_modules/.bin easy.

@greypants
Copy link

Another solution is to use the scripts property in package.json and npm run yourthing instead. npm run first looks for scripts local to that folder in node_modules/.bin

"scripts": {
  "gulp":  "npm run gulp"
}

Then you can

npm run gulp

You can still pass arguments too:

npm run gulp someTask

No more global installs evar.

@shyam-habarakada
Copy link

A cleaner solution, as someone pointed out in stack overflow is to simply update your path as,

export PATH=./node_modules/.bin:$PATH

This limits you to finding the binaries only while running commands from a project root directory (or the same parent folder as node_modules) but it seems better than other solutions that involve running script on PROMPT_COMMAND, etc.

@airtonix
Copy link

No. You should use either nvm or nodist.
You should then be using the scripts feature of your package.json.

If you're using nvm then you should be using autoenv.

Don't mess with your path at runtime, is not reproducible, not robust and
you can't predict how it will pan out on other systems.

On Fri, 20 May 2016, 07:10 shyam-habarakada notifications@github.com
wrote:

A cleaner solution, as someone pointed out in stack overflow is to simply
update your path as,

export PATH=./node_modules/.bin:$PATH

This limits you to finding the binaries only while running commands from a
project root directory (or the same parent folder as node_modules) but it
seems better than other solutions that involve running script on
PROMPT_COMMAND, etc.


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#957 (comment)

@nylen
Copy link

nylen commented Aug 2, 2016

Another reason not to modify your path like that is that a broken (or even malicious) package can overwrite a system command and lead to problems that can be very difficult to debug. Example: isaacs/node-touch#8

If you must add node_modules/.bin to your $PATH, do it after other directories, not before.

export PATH="$PATH:./node_modules/.bin"

@dheerajbhaskar
Copy link

For Windows

Store the following in a file called npm-exec.bat and add it to %PATH%

@echo off
set cmd="npm bin"
FOR /F "tokens=" %%i IN (' %cmd% ') DO SET modules=%%i
"%modules%%
"

Usage

Then you can use it like npm-exec wdio to execute wdio installed in local node_modules directory i.e. it will run .\node_modules\.bin\wdio

@pnaszarkowski
Copy link

Another solution is to use the scripts property in package.json and npm run yourthing instead. npm run first looks for scripts local to that folder in node_modules/.bin
"scripts": {
"gulp": "npm run gulp"
}
Then you can
npm run gulp

@greypants seems like you will end up with undefinite loop here. Shouldn't this be:

"scripts": {
    "gulp":  "./node_modules/.bin/gulp"
  }

?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

9 participants