npm-path will get you a PATH with all of the executables available to npm scripts, without booting up all of npm(1).
- All of the
node_modules/.bindirectories from the current directory, up through all of its parents. This allows you to invoke the executables for any installed modules. e.g. ifmochais installed a dependency of the current module, thenmochawill be available on anpm-pathgenerated$PATH. - The directory containing the current
nodeexecutable, so any scripts that invokenodewill execute the samenode. - Current npm's
node-gypdirectory, so thenode-gypbundled withnpmcan be used.
# 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: ... etcCalling 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.
This will set the augmented PATH for the current process environment, or an environment you supply.
var npmPath = require('npm-path')
var PATH = npmPath.PATH // get platform independent PATH key
npmPath(function(err, $PATH) {
// Note: current environment is modified!
console.log(process.env[PATH] == $PATH) // true
console.log($PATH)
// /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/.../.bin:/usr/local/bin: ...etc
})
// more explicit alternative syntax
npmPath.set(function(err, $PATH) {
// ...
})// supplying no callback will execute method synchronously
var $PATH = npmPath()
console.log($PATH)
// more explicit alternative syntax
$PATH = npmPath.setSync()var options = {
env: process.env, // default.
cwd: process.cwd() // default.
}
npmPath(options, function(err, $PATH) {
// ...
})
npmPath.setSync(options)
// ...This will get npm augmented PATH, but does not modify the PATH in the environment.
Takes the exact same options as set.
npmPath.get(function(err, $PATH) {
console.log($PATH)
// Note: current environment is NOT modified!
console.log(process.env[PATH] == $PATH) // false
})
// options is optional, takes same options as `npmPath.set`
npmPath.get(options, function(err, $PATH) {
console.log($PATH)
})// supplying no callback will execute method synchronously
var $PATH = npmPath.get()
console.log($PATH)
console.log(process.env[PATH] == $PATH) // false
// more explicit alternative syntax
$PATH = npmPath.getSync()Both set and get take an optional options object, with optional env & cwd keys.
- Set
options.envif you wish to use something other thanprocess.env(the default) - Set
options.cwdif 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.
// windows calls it's path "Path" usually, but this is not guaranteed.
npmPath.PATH // 'Path', probably
// rest of the world
npmPath.PATH // 'PATH'process.env[npmPath.PATH] // get path environment variable
// set path environment variable manually
process.env[npmPath.PATH] = npmPath.get()
// set path environment variable automatically
npmPath()// windows
npmPath.SEPARATOR // ';'
// rest of the world
npmPath.SEPARATOR // ':'Path lookup code adapted directly from npm.
Thanks to Jordan Harband for his hard work adapting this to work on node 0.8.
MIT