6310c6b58b08db5964a50504d3015bb783b0fac9
[platform/framework/web/crosswalk-tizen.git] /
1 # npm-path
2
3 ### Get a PATH containing locally installed module executables.
4
5 `npm-path` will get you a PATH with all of the executables available to npm scripts, without booting up all of npm(1).
6
7 #### `npm-path` will set your PATH to include:
8
9 * 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. if `mocha` is installed a dependency of the current module, then `mocha` will be available on a `npm-path` generated `$PATH`.
10 * The directory containing the current `node` executable, so any scripts that invoke `node` will execute the same `node`.
11 * Current npm's `node-gyp` directory, so the `node-gyp` bundled with `npm` can be used.
12
13 ## Usage
14
15 ### Command-line
16
17 ```bash
18 # Prints the augmented PATH to the console
19 > npm-path
20 # /usr/local/lib/node_modules/npm/bin/node-gyp-bin:.../node_modules/.bin:/.../usr/local/bin:/usr/local/sbin: ... etc
21 ```
22
23 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`.
24
25 ### Set PATH
26
27 This will set the augmented PATH for the current process environment, or an environment you supply.
28
29 ```js
30 var npmPath = require('npm-path')
31 var PATH = npmPath.PATH // get platform independent PATH key
32
33 npmPath(function(err, $PATH) {
34   
35   // Note: current environment is modified!
36   console.log(process.env[PATH] == $PATH) // true
37   
38   console.log($PATH)
39   // /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/.../.bin:/usr/local/bin: ...etc
40   
41 })
42
43 // more explicit alternative syntax
44 npmPath.set(function(err, $PATH) {
45   // ...
46 })
47 ```
48
49 #### Synchronous Alternative
50
51 ```js
52
53 //  supplying no callback will execute method synchronously
54 var $PATH = npmPath()
55 console.log($PATH)
56
57 // more explicit alternative syntax
58 $PATH = npmPath.setSync()
59 ```
60
61 #### Optional Options
62
63 ```js
64 var options = {
65   env: process.env, // default.
66   cwd: process.cwd() // default.
67 }
68
69 npmPath(options, function(err, $PATH) {
70   // ...
71 })
72
73 npmPath.setSync(options)
74
75   // ...
76
77 ```
78
79
80 ### Get PATH
81
82 This will get npm augmented PATH, but *does not modify the PATH in the environment*.
83 Takes the exact same options as `set`.
84
85 ```js
86 npmPath.get(function(err, $PATH) {
87   console.log($PATH)
88   
89   // Note: current environment is NOT modified!
90   console.log(process.env[PATH] == $PATH) // false
91 })
92
93 // options is optional, takes same options as `npmPath.set`
94 npmPath.get(options, function(err, $PATH) {
95   console.log($PATH)
96 })
97 ```
98
99 #### Synchronous Alternative
100
101 ```js
102 //  supplying no callback will execute method synchronously
103 var $PATH = npmPath.get()
104 console.log($PATH)
105 console.log(process.env[PATH] == $PATH) // false
106
107 // more explicit alternative syntax
108 $PATH = npmPath.getSync()
109
110 ```
111
112 ### Options
113
114 Both `set` and `get` take an optional options object, with optional `env` & `cwd` keys.
115
116 * Set `options.env` if you wish to use something other than `process.env` (the default)
117 * Set `options.cwd` if you wish to use something other than `process.cwd()` (the default)
118
119 There's also a `options.npm` property which you can set if you want `node-gyp` to be sourced from
120 an alternative `npm` installation.
121
122 ### Get the PATH environment variable key
123
124 ```js
125 // windows calls it's path "Path" usually, but this is not guaranteed.
126 npmPath.PATH // 'Path', probably
127
128 // rest of the world
129 npmPath.PATH // 'PATH'
130
131 ```
132
133 #### Example Usage
134
135 ```js
136 process.env[npmPath.PATH] // get path environment variable
137
138 // set path environment variable manually
139 process.env[npmPath.PATH] = npmPath.get()
140
141 // set path environment variable automatically
142 npmPath()
143 ```
144
145 ### Get the PATH separator
146
147 ```js
148 // windows
149 npmPath.SEPARATOR // ';'
150
151 // rest of the world
152 npmPath.SEPARATOR // ':'
153 ```
154
155 ## Credit
156
157 Path lookup code adapted directly from npm.
158
159 # License
160
161 MIT