1

I run a lot of node projects and often have binaries located in:

.\node_modules\.bin

...relative to the projects folder. I'd like to be able to have PATH always include these directories, if they exist. I don't want to include other directories, just the one relative to the current directory. I'm familiar with Add-PathVariable from PSCX and other Powershell basics, but how do I include a folder relative to the current dir in PATH?

Edit: as mentioned in the question, already, I expect the path to stay updated as the directory changes. This is not simply asking about how to use pwd.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • You should be able to do this by simply adding ".\node_modules\.bin" (you may have to double the backslashes; I'm never quite sure) to the system PATH. – Jeff Zeitlin Jul 24 '17 at 12:39
  • 1
    `.` is not the same as `$(pwd)`. – Ansgar Wiechers Jul 24 '17 at 13:16
  • @AnsgarWiechers Your comment was rude but you're correct: you can add relative folders to PATH (I didn't realise this). Write it and I'll mark it as correct. – mikemaccana Jul 24 '17 at 15:06
  • I consider it due diligence here on SO to actually test the subject of a question *before* asking the question. Particularly for someone with almost 30k rep. I do not consider it rude at all when neglect of said due diligence is pointed out. YMMV. – Ansgar Wiechers Jul 24 '17 at 19:34
  • @AnsgarWiechers I considered '.' but just assumed '.' would resolve immediately (ie, that it was static rather than dynamic). Re: due diligence: I wrote a somewhat complex script using `find-string` and `prompt` to do this dynamically before you posted your answer-comment. You could have made the same point more politely, likewise I'd expect better from someone with over 100K karma. – mikemaccana Jul 25 '17 at 12:39

1 Answers1

2

You can use a relative path in Env:PATH and the binaries found will update dynamically:

Eg:

$env:PATH += ';.\node_modules\.bin'

Or with the PowerShell Community Extensions (PSCX):

Add-PathVariable '.\node_modules\.bin'

Unlike using $(pwd) the . is not immediately resolved to an absolute path, so PATH is always relative to the current working directory.

Testing this:

$ which uuid
C:\Users\username\Documents\myapp\node_modules\.bin\uuid.cmd

Then changing directory, uuid now refers to a program in a different dir:

$ cd ..\blog\
$ which uuid
C:\Users\username\Documents\blog\node_modules\.bin\uuid.cmd

It's also possible to persistently change PATH in the user or system environment:

[Environment]::SetEnvironmentVariable(($env:PATH + ';.'), 'User')

or

[Environment]::SetEnvironmentVariable(($env:PATH + ';.'), 'Machine')

Security note: when entering a command Windows will automatically search all directories in $env:PATH for files with one of the extensions listed in $env:PATHEXT and execute the first match it finds. Depending on where exactly in the search path you placed . that may even supersede system executables.

You may want to take a look at how to use package installed locally in node_modules for alternative approaches.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328