npm-path
Get a PATH containing locally installed module executables.
npm-path
will get you a PATH with all of the executables available to npm scripts, without booting up all of npm(1).
npm-path
will set your PATH to include:
- All of the
node_modules/.bin
directories from the current directory, up through all of its parents. This allows you to invoke the executables for any installed modules. e.g. ifmocha
is installed a dependency of the current module, thenmocha
will be available on anpm-path
generated$PATH
. - The directory containing the current
node
executable, so any scripts that invokenode
will execute the samenode
. - Current npm's
node-gyp
directory, so thenode-gyp
bundled withnpm
can be used.
Usage
Command-line
# Prints the augmented PATH to the console > npm-path# /usr/local/lib/node_modules/npm/bin/node-gyp-bin:.../node_modules/.bin:/.../usr/local/bin:/usr/local/sbin: ... etc
Calling npm-path
from the commandline is the equivalent of executing an npm script with the body echo $PATH
, but without all of the overhead of booting or depending on npm
.
Set PATH
This will set the augmented PATH for the current process environment, or an environment you supply.
var npmPath = var PATH = npmPathPATH // get platform independent PATH key // more explicit alternative syntaxnpmPath
Synchronous Alternative
// supplying no callback will execute method synchronouslyvar $PATH = console // more explicit alternative syntax$PATH = npmPath
Optional Options
var options = env: processenv // default. cwd: process // default. npmPath // ...
Get PATH
This will get npm augmented PATH, but does not modify the PATH in the environment.
Takes the exact same options as set
.
npmPath // options is optional, takes same options as `npmPath.set`npmPath
Synchronous Alternative
// supplying no callback will execute method synchronouslyvar $PATH = npmPathconsoleconsole // false // more explicit alternative syntax$PATH = npmPath
Options
Both set
and get
take an optional options object, with optional env
& cwd
keys.
- Set
options.env
if you wish to use something other thanprocess.env
(the default) - Set
options.cwd
if you wish to use something other thanprocess.cwd()
(the default)
There's also a options.npm
property which you can set if you want node-gyp
to be sourced from
an alternative npm
installation.
Get the PATH environment variable key
// windows calls it's path "Path" usually, but this is not guaranteed.npmPathPATH // 'Path', probably // rest of the worldnpmPathPATH // 'PATH'
Example Usage
processenvnpmPathPATH // get path environment variable // set path environment variable manuallyprocessenvnpmPathPATH = npmPath // set path environment variable automatically
Get the PATH separator
// windowsnpmPathSEPARATOR // ';' // rest of the worldnpmPathSEPARATOR // ':'
Credit
Path lookup code adapted directly from npm.
Thanks to Jordan Harband for his hard work adapting this to work on node 0.8.
License
MIT