node/no-path-concat Restriction
What it does
Disallows string concatenation with __dirname and __filename.
Why is this bad?
In 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:
js
var fullPath = __dirname + "/foo.js";However, this is error-prone because it doesn't account for different operating systems, which use different path separators. Using path.join() or path.resolve() is the proper way to create cross-platform file paths.
Examples
Examples of incorrect code for this rule:
js
const fullPath1 = __dirname + "/foo.js";
const fullPath2 = __filename + "/foo.js";
const fullPath3 = `${__dirname}/foo.js`;
const fullPath4 = `${__filename}/foo.js`;Examples of correct code for this rule:
js
const fullPath1 = path.join(__dirname, "foo.js");
const fullPath2 = path.join(__filename, "foo.js");
const fullPath3 = __dirname + ".js";
const fullPath4 = __filename + ".map";
const fullPath5 = `${__dirname}_foo.js`;
const fullPath6 = `${__filename}.test.js`;How to use
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["node"],
"rules": {
"node/no-path-concat": "error"
}
}bash
oxlint --deny node/no-path-concat --node-plugin