n/no-path-concat
Disallow joining __dirname and __filename to a path with + or a template
Also called node/no-path-concat
When you glue a path together by hand, you hardcode the separator. On Windows, __dirname + "/data.json" gives C:\app/data.json, which isn't equal to the same path from path.join(). path.join() and path.resolve() use the platform's separator and clean up .. and doubled separators.
The rule reports __dirname or __filename followed by text that starts with / or \, or by path.sep.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"n/no-path-concat" = "error" # or "warn"Examples#
example.js3 problems
const config = __dirname + "/config.json"Use path.join() or path.resolve() instead of string concatenation;
const cache = `${__dirname}/cache/${name}`Use path.join() or path.resolve() instead of string concatenation;
const logs = __dirname + path.sepUse path.join() or path.resolve() instead of string concatenation + "logs";example.jsNo problems
const path = require("path");
const config = path.join(__dirname, "config.json");
const cache = path.resolve(__dirname, "cache", name);Adding something that isn't a new path segment is fine:
example.jsNo problems
const sourceMap = __filename + ".map";