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

Can we add locally installed packages to PATH, too? #8221

Closed
donaldpipowitch opened this issue May 9, 2015 · 4 comments
Closed

Can we add locally installed packages to PATH, too? #8221

donaldpipowitch opened this issue May 9, 2015 · 4 comments

Comments

@donaldpipowitch
Copy link

I have the feeling that the Node community has still problems figuring out how to handle locally vs. globally installed packages and binaries despite talking about this in detail for years (like here in the official Node blog).

I think packages like grunt-cli are a good example of this. The most recent example of this (for me) was this discussion about why it is good or bad to export Karmas binary.

So what's the problem? At least that's how I understand it... A globally installed package is just so much easier to use. But besides that, we rarely really need a global package. We want a package with a specific version and configuration for all our projects - which a locally installed package solves for us. But a locally installed package isn't that easy to use. We can call node_modules/.bin/foo or we can alias the executable in the "scripts" part of our package.json and call something like npm run foo. The last way is sometimes a little bit easier and we can preset some params (like foo --bar) and it works anywhere inside the current package, but it's more verbose (and uncommon for many) to pass params with the -- limiter (see #3494). The first way is simpler to use with params, but we need to rewrite the path if we are in subdirectories of the current package. No much difference between both ways, but it's not as simple as typing foo.
So my question: Can we find a way to add locally installed packages to PATH, too? An good reasons against that? Can't we just recursively add all executables to PATH dependent on our current working directory? That would very much fit Nodes require logic.
It sound so cool (imo). Install foo locally and call foo in our current project. If we are in a different project without foo, we could fallback to a globally installed foo.

@rlidwka
Copy link
Contributor

rlidwka commented May 9, 2015

You can run:

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

To add all those directories to PATH. I know no way of adding all folders recursively up to the top, but enumerating first few paths explicitly seems to be good enough.

So yes. I wonder if there are any unwanted side-effects though.

@donaldpipowitch
Copy link
Author

The user pyrocrasty helped on SO to find a solution for this:

RAW_PATH="$PATH"
LAST_WD=`pwd`

augment_path() {
    target="node_modules/.bin"
    if [ "$PWD" = "$LAST_WD" ]; then return 0; fi;
    PATH_ADDITION=""
    scandir="$PWD"
    until [ "$scandir" = "" ]; do
    resolved_target="$scandir"/"$target"
    if [ -d "$resolved_target" ]; then
        PATH_ADDITION="$PATH_ADDITION:$resolved_target"
    fi
    scandir="${scandir%/*}"
    done 
    PATH="$PATH_ADDITION:$RAW_PATH"
    LAST_WD=`pwd`
}

PROMPT_COMMAND_OLD="${PROMPT_COMMAND%; augment_path}"
PROMPT_COMMAND="$PROMPT_COMMAND_OLD; augment_path"

This works just wonderful for experimenting. I really hope we could make this the default behavior.

@othiym23 othiym23 changed the title Question: Can we add locally installed packages to PATH, too? Can we add locally installed packages to PATH, too? Jul 8, 2015
@othiym23
Copy link
Contributor

othiym23 commented Mar 3, 2016

Setting up the paths is a big part of using package scripts. There are a bunch of good reasons related to trust and security to not just add arbitrary directories relative to the current path onto your PATH. That said, an alternative way to get at local scripts would be the much-mooted npm-exec helper binary, as discussed in #6053. As such, I'm going to close this issue as a duplicate of that one, with the corollary that the team (especially including me) really doesn't think it' s a good idea to tag ./node_modules/.bin onto your current path, which is part of the reason why npm will never export locally-installed binaries onto your path itself. This is pretty much the primary reason that global installs exist.

Thanks for your time, and your thoughtful description of the problem you're trying to solve.

@othiym23 othiym23 closed this as completed Mar 3, 2016
@donaldpipowitch
Copy link
Author

Thanks. I subscribed to the linked issue now.

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

No branches or pull requests

3 participants