]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-path-concat.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-path-concat.md
CommitLineData
eb39fafa
DC
1# Disallow string concatenation when using `__dirname` and `__filename` (no-path-concat)
2
56c4a2cb
DC
3This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-node`](https://github.com/mysticatea/eslint-plugin-node).
4
eb39fafa
DC
5In Node.js, the `__dirname` and `__filename` global variables contain the directory path and the file path of the currently executing script file, respectively. Sometimes, developers try to use these variables to create paths to other files, such as:
6
7```js
8var fullPath = __dirname + "/foo.js";
9```
10
11However, there are a few problems with this. First, you can't be sure what type of system the script is running on. Node.js can be run on any computer, including Windows, which uses a different path separator. It's very easy, therefore, to create an invalid path using string concatenation and assuming Unix-style separators. There's also the possibility of having double separators, or otherwise ending up with an invalid path.
12
13In order to avoid any confusion as to how to create the correct path, Node.js provides the `path` module. This module uses system-specific information to always return the correct value. So you can rewrite the previous example as:
14
15```js
16var fullPath = path.join(__dirname, "foo.js");
17```
18
19This example doesn't need to include separators as `path.join()` will do it in the most appropriate manner. Alternately, you can use `path.resolve()` to retrieve the fully-qualified path:
20
21```js
22var fullPath = path.resolve(__dirname, "foo.js");
23```
24
25Both `path.join()` and `path.resolve()` are suitable replacements for string concatenation wherever file or directory paths are being created.
26
27## Rule Details
28
29This rule aims to prevent string concatenation of directory paths in Node.js
30
31Examples of **incorrect** code for this rule:
32
33```js
34/*eslint no-path-concat: "error"*/
35
36var fullPath = __dirname + "/foo.js";
37
38var fullPath = __filename + "/foo.js";
39
40```
41
42Examples of **correct** code for this rule:
43
44```js
45/*eslint no-path-concat: "error"*/
46
47var fullPath = dirname + "/foo.js";
48```
49
50## When Not To Use It
51
52If you want to allow string concatenation of path names.