Skip to content

Commit d08463a

Browse files
sam-githubevanlucas
authored andcommitted
doc: unify dirname and filename description
__dirname is path.dirname(__filename), but its docs, specifically the attempt to describe javascript scope in terms of "running" and "executing" had drifted apart. Rework to describe one as a variation of the other, move the example, and just describe the names in terms of the module, and it's local variables rather than the ill defined execution concepts. Fix: #5525 PR-URL: #10527 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent 659d522 commit d08463a

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

doc/api/globals.md

+32-19
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,20 @@ added: v0.1.27
2929

3030
* {String}
3131

32-
The name of the directory that the currently executing script resides in.
32+
The directory name of the current module. This the same as the
33+
[`path.dirname()`][] of the [`__filename`][].
34+
35+
`__dirname` isn't actually a global but rather local to each module.
3336

3437
Example: running `node example.js` from `/Users/mjr`
3538

3639
```js
3740
console.log(__dirname);
3841
// Prints: /Users/mjr
42+
console.log(path.dirname(__filename));
43+
// Prints: /Users/mjr
3944
```
4045

41-
`__dirname` isn't actually a global but rather local to each module.
42-
43-
For instance, given two modules: `a` and `b`, where `b` is a dependency of
44-
`a` and there is a directory structure of:
45-
46-
* `/Users/mjr/app/a.js`
47-
* `/Users/mjr/app/node_modules/b/b.js`
48-
49-
References to `__dirname` within `b.js` will return
50-
`/Users/mjr/app/node_modules/b` while references to `__dirname` within `a.js`
51-
will return `/Users/mjr/app`.
52-
5346
## \_\_filename
5447
<!-- YAML
5548
added: v0.0.1
@@ -59,19 +52,36 @@ added: v0.0.1
5952

6053
* {String}
6154

62-
The filename of the code being executed. This is the resolved absolute path
63-
of this code file. For a main program this is not necessarily the same
64-
filename used in the command line. The value inside a module is the path
65-
to that module file.
55+
The file name of the current module. This is the resolved absolute path of the
56+
current module file.
6657

67-
Example: running `node example.js` from `/Users/mjr`
58+
For a main program this is not necessarily the same as the file name used in the
59+
command line.
60+
61+
See [`__dirname`][] for the directory name of the current module.
62+
63+
`__filename` isn't actually a global but rather local to each module.
64+
65+
Examples:
66+
67+
Running `node example.js` from `/Users/mjr`
6868

6969
```js
7070
console.log(__filename);
7171
// Prints: /Users/mjr/example.js
72+
console.log(__dirname);
73+
// Prints: /Users/mjr
7274
```
7375

74-
`__filename` isn't actually a global but rather local to each module.
76+
Given two modules: `a` and `b`, where `b` is a dependency of
77+
`a` and there is a directory structure of:
78+
79+
* `/Users/mjr/app/a.js`
80+
* `/Users/mjr/app/node_modules/b/b.js`
81+
82+
References to `__filename` within `b.js` will return
83+
`/Users/mjr/app/node_modules/b/b.js` while references to `__filename` within
84+
`a.js` will return `/Users/mjr/app/a.js`.
7585

7686
## clearImmediate(immediateObject)
7787
<!-- YAML
@@ -262,7 +272,10 @@ added: v0.0.1
262272

263273
[`setTimeout`] is described in the [timers][] section.
264274

275+
[`__dirname`]: #globals_dirname
276+
[`__filename`]: #globals_filename
265277
[`console`]: console.html
278+
[`path.dirname()`]: path.html#path_path_dirname_path
266279
[`process` object]: process.html#process_process
267280
[buffer section]: buffer.html
268281
[module system documentation]: modules.html

0 commit comments

Comments
 (0)