32493d36e12c7f3c286add91b5af13f476315a98
[platform/upstream/nodejs.git] / deps / npm / doc / misc / npm-faq.md
1 npm-faq(7) -- Frequently Asked Questions
2 ========================================
3
4 ## Where can I find these docs in HTML?
5
6 <https://npmjs.org/doc/>, or run:
7
8     npm config set viewer browser
9
10 to open these documents in your default web browser rather than `man`.
11
12 ## It didn't work.
13
14 That's not really a question.
15
16 ## Why didn't it work?
17
18 I don't know yet.
19
20 Read the error output, and if you can't figure out what it means,
21 do what it says and post a bug with all the information it asks for.
22
23 ## Where does npm put stuff?
24
25 See `npm-folders(5)`
26
27 tl;dr:
28
29 * Use the `npm root` command to see where modules go, and the `npm bin`
30   command to see where executables go
31 * Global installs are different from local installs.  If you install
32   something with the `-g` flag, then its executables go in `npm bin -g`
33   and its modules go in `npm root -g`.
34
35 ## How do I install something on my computer in a central location?
36
37 Install it globally by tacking `-g` or `--global` to the command.  (This
38 is especially important for command line utilities that need to add
39 their bins to the global system `PATH`.)
40
41 ## I installed something globally, but I can't `require()` it
42
43 Install it locally.
44
45 The global install location is a place for command-line utilities
46 to put their bins in the system `PATH`.  It's not for use with `require()`.
47
48 If you `require()` a module in your code, then that means it's a
49 dependency, and a part of your program.  You need to install it locally
50 in your program.
51
52 ## Why can't npm just put everything in one place, like other package managers?
53
54 Not every change is an improvement, but every improvement is a change.
55 This would be like asking git to do network IO for every commit.  It's
56 not going to happen, because it's a terrible idea that causes more
57 problems than it solves.
58
59 It is much harder to avoid dependency conflicts without nesting
60 dependencies.  This is fundamental to the way that npm works, and has
61 proven to be an extremely successful approach.  See `npm-folders(5)` for
62 more details.
63
64 If you want a package to be installed in one place, and have all your
65 programs reference the same copy of it, then use the `npm link` command.
66 That's what it's for.  Install it globally, then link it into each
67 program that uses it.
68
69 ## Whatever, I really want the old style 'everything global' style.
70
71 Write your own package manager, then.  It's not that hard.
72
73 npm will not help you do something that is known to be a bad idea.
74
75 ## Should I check my `node_modules` folder into git?
76
77 Mikeal Rogers answered this question very well:
78
79 <http://www.mikealrogers.com/posts/nodemodules-in-git.html>
80
81 tl;dr
82
83 * Check `node_modules` into git for things you **deploy**, such as
84   websites and apps.
85 * Do not check `node_modules` into git for libraries and modules
86   intended to be reused.
87 * Use npm to manage dependencies in your dev environment, but not in
88   your deployment scripts.
89
90 ## Is it 'npm' or 'NPM' or 'Npm'?
91
92 npm should never be capitalized unless it is being displayed in a
93 location that is customarily all-caps (such as the title of man pages.)
94
95 ## If 'npm' is an acronym, why is it never capitalized?
96
97 Contrary to the belief of many, "npm" is not in fact an abbreviation for
98 "Node Package Manager".  It is a recursive bacronymic abbreviation for
99 "npm is not an acronym".  (If it was "ninaa", then it would be an
100 acronym, and thus incorrectly named.)
101
102 "NPM", however, *is* an acronym (more precisely, a capitonym) for the
103 National Association of Pastoral Musicians.  You can learn more
104 about them at <http://npm.org/>.
105
106 In software, "NPM" is a Non-Parametric Mapping utility written by
107 Chris Rorden.  You can analyze pictures of brains with it.  Learn more
108 about the (capitalized) NPM program at <http://www.cabiatl.com/mricro/npm/>.
109
110 The first seed that eventually grew into this flower was a bash utility
111 named "pm", which was a shortened descendent of "pkgmakeinst", a
112 bash function that was used to install various different things on different
113 platforms, most often using Yahoo's `yinst`.  If `npm` was ever an
114 acronym for anything, it was `node pm` or maybe `new pm`.
115
116 So, in all seriousness, the "npm" project is named after its command-line
117 utility, which was organically selected to be easily typed by a right-handed
118 programmer using a US QWERTY keyboard layout, ending with the
119 right-ring-finger in a postition to type the `-` key for flags and
120 other command-line arguments.  That command-line utility is always
121 lower-case, though it starts most sentences it is a part of.
122
123 ## How do I list installed packages?
124
125 `npm ls`
126
127 ## How do I search for packages?
128
129 `npm search`
130
131 Arguments are greps.  `npm search jsdom` shows jsdom packages.
132
133 ## How do I update npm?
134
135     npm update npm -g
136
137 You can also update all outdated local packages by doing `npm update` without
138 any arguments, or global packages by doing `npm update -g`.
139
140 Occasionally, the version of npm will progress such that the current
141 version cannot be properly installed with the version that you have
142 installed already.  (Consider, if there is ever a bug in the `update`
143 command.)
144
145 In those cases, you can do this:
146
147     curl https://npmjs.org/install.sh | sh
148
149 ## What is a `package`?
150
151 A package is:
152
153 * a) a folder containing a program described by a package.json file
154 * b) a gzipped tarball containing (a)
155 * c) a url that resolves to (b)
156 * d) a `<name>@<version>` that is published on the registry with (c)
157 * e) a `<name>@<tag>` that points to (d)
158 * f) a `<name>` that has a "latest" tag satisfying (e)
159 * g) a `git` url that, when cloned, results in (a).
160
161 Even if you never publish your package, you can still get a lot of
162 benefits of using npm if you just want to write a node program (a), and
163 perhaps if you also want to be able to easily install it elsewhere
164 after packing it up into a tarball (b).
165
166 Git urls can be of the form:
167
168     git://github.com/user/project.git#commit-ish
169     git+ssh://user@hostname:project.git#commit-ish
170     git+http://user@hostname/project/blah.git#commit-ish
171     git+https://user@hostname/project/blah.git#commit-ish
172
173 The `commit-ish` can be any tag, sha, or branch which can be supplied as
174 an argument to `git checkout`.  The default is `master`.
175
176 ## What is a `module`?
177
178 A module is anything that can be loaded with `require()` in a Node.js
179 program.  The following things are all examples of things that can be
180 loaded as modules:
181
182 * A folder with a `package.json` file containing a `main` field.
183 * A folder with an `index.js` file in it.
184 * A JavaScript file.
185
186 Most npm packages are modules, because they are libraries that you
187 load with `require`.  However, there's no requirement that an npm
188 package be a module!  Some only contain an executable command-line
189 interface, and don't provide a `main` field for use in Node programs.
190
191 Almost all npm packages (at least, those that are Node programs)
192 *contain* many modules within them (because every file they load with
193 `require()` is a module).
194
195 In the context of a Node program, the `module` is also the thing that
196 was loaded *from* a file.  For example, in the following program:
197
198     var req = require('request')
199
200 we might say that "The variable `req` refers to the `request` module".
201
202 ## So, why is it the "`node_modules`" folder, but "`package.json`" file?  Why not `node_packages` or `module.json`?
203
204 The `package.json` file defines the package.  (See "What is a
205 package?" above.)
206
207 The `node_modules` folder is the place Node.js looks for modules.
208 (See "What is a module?" above.)
209
210 For example, if you create a file at `node_modules/foo.js` and then
211 had a program that did `var f = require('foo.js')` then it would load
212 the module.  However, `foo.js` is not a "package" in this case,
213 because it does not have a package.json.
214
215 Alternatively, if you create a package which does not have an
216 `index.js` or a `"main"` field in the `package.json` file, then it is
217 not a module.  Even if it's installed in `node_modules`, it can't be
218 an argument to `require()`.
219
220 ## `"node_modules"` is the name of my deity's arch-rival, and a Forbidden Word in my religion.  Can I configure npm to use a different folder?
221
222 No.  This will never happen.  This question comes up sometimes,
223 because it seems silly from the outside that npm couldn't just be
224 configured to put stuff somewhere else, and then npm could load them
225 from there.  It's an arbitrary spelling choice, right?  What's the big
226 deal?
227
228 At the time of this writing, the string `'node_modules'` appears 151
229 times in 53 separate files in npm and node core (excluding tests and
230 documentation).
231
232 Some of these references are in node's built-in module loader.  Since
233 npm is not involved **at all** at run-time, node itself would have to
234 be configured to know where you've decided to stick stuff.  Complexity
235 hurdle #1.  Since the Node module system is locked, this cannot be
236 changed, and is enough to kill this request.  But I'll continue, in
237 deference to your deity's delicate feelings regarding spelling.
238
239 Many of the others are in dependencies that npm uses, which are not
240 necessarily tightly coupled to npm (in the sense that they do not read
241 npm's configuration files, etc.)  Each of these would have to be
242 configured to take the name of the `node_modules` folder as a
243 parameter.  Complexity hurdle #2.
244
245 Furthermore, npm has the ability to "bundle" dependencies by adding
246 the dep names to the `"bundledDependencies"` list in package.json,
247 which causes the folder to be included in the package tarball.  What
248 if the author of a module bundles its dependencies, and they use a
249 different spelling for `node_modules`?  npm would have to rename the
250 folder at publish time, and then be smart enough to unpack it using
251 your locally configured name.  Complexity hurdle #3.
252
253 Furthermore, what happens when you *change* this name?  Fine, it's
254 easy enough the first time, just rename the `node_modules` folders to
255 `./blergyblerp/` or whatever name you choose.  But what about when you
256 change it again?  npm doesn't currently track any state about past
257 configuration settings, so this would be rather difficult to do
258 properly.  It would have to track every previous value for this
259 config, and always accept any of them, or else yesterday's install may
260 be broken tomorrow.  Complexity hurdle #5.
261
262 Never going to happen.  The folder is named `node_modules`.  It is
263 written indelibly in the Node Way, handed down from the ancient times
264 of Node 0.3.
265
266 ## How do I install node with npm?
267
268 You don't.  Try one of these node version managers:
269
270 Unix:
271
272 * <http://github.com/isaacs/nave>
273 * <http://github.com/visionmedia/n>
274 * <http://github.com/creationix/nvm>
275
276 Windows:
277
278 * <http://github.com/marcelklehr/nodist>
279 * <https://github.com/hakobera/nvmw>
280
281 ## How can I use npm for development?
282
283 See `npm-developers(7)` and `package.json(5)`.
284
285 You'll most likely want to `npm link` your development folder.  That's
286 awesomely handy.
287
288 To set up your own private registry, check out `npm-registry(7)`.
289
290 ## Can I list a url as a dependency?
291
292 Yes.  It should be a url to a gzipped tarball containing a single folder
293 that has a package.json in its root, or a git url.
294 (See "what is a package?" above.)
295
296 ## How do I symlink to a dev folder so I don't have to keep re-installing?
297
298 See `npm-link(1)`
299
300 ## The package registry website.  What is that exactly?
301
302 See `npm-registry(7)`.
303
304 ## I forgot my password, and can't publish.  How do I reset it?
305
306 Go to <https://npmjs.org/forgot>.
307
308 ## I get ECONNREFUSED a lot.  What's up?
309
310 Either the registry is down, or node's DNS isn't able to reach out.
311
312 To check if the registry is down, open up <http://registry.npmjs.org/>
313 in a web browser.  This will also tell you if you are just unable to
314 access the internet for some reason.
315
316 If the registry IS down, let me know by emailing <i@izs.me> or posting
317 an issue at <https://github.com/isaacs/npm/issues>.  We'll have
318 someone kick it or something.
319
320 ## Why no namespaces?
321
322 Please see this discussion: <https://github.com/isaacs/npm/issues/798>
323
324 tl;dr - It doesn't actually make things better, and can make them worse.
325
326 If you want to namespace your own packages, you may: simply use the
327 `-` character to separate the names.  npm is a mostly anarchic system.
328 There is not sufficient need to impose namespace rules on everyone.
329
330 ## Who does npm?
331
332 `npm view npm author`
333
334 `npm view npm contributors`
335
336 ## I have a question or request not addressed here. Where should I put it?
337
338 Post an issue on the github project:
339
340 * <https://github.com/isaacs/npm/issues>
341
342 ## Why does npm hate me?
343
344 npm is not capable of hatred.  It loves everyone, especially you.
345
346 ## SEE ALSO
347
348 * npm(1)
349 * npm-developers(7)
350 * package.json(5)
351 * npm-config(1)
352 * npm-config(7)
353 * npmrc(5)
354 * npm-config(7)
355 * npm-folders(5)