noPathConcat (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/noPathConcat - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
n/no-path-concat
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noPathConcat": "error" } } }}Description
Section titled “Description”Disallow building file paths and URLs by joining strings to __dirname, __filename, or import.meta values.
In Node.js, __dirname and __filename hold the folder and the path of the current file.
import.meta.dirname and import.meta.filename hold the same values in ES modules.
Adding a string to one of them is a common way to point at another file,
but the character that separates folders depends on the operating system:
Windows uses \, while Linux and macOS use /.
Joining strings can also leave two separators in a row.
Use path.join() or path.resolve() from the node:path module instead.
They insert the right separator for the system the code runs on.
import.meta.url is a URL such as file:///project/src/index.js, not a file path.
Adding /foo.js to it gives file:///project/src/index.js/foo.js, which doesn’t point to a file next to the current one.
Use new URL() to build a URL that is relative to import.meta.url.
The rule reports one of these values only when the text added right after it starts with /, \,
or the sep value of the node:path module.
Adding other text, such as a file extension, is allowed.
The rule doesn’t look up the value of a variable that is added, so __dirname + suffix is never reported.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const fullPath = __dirname + "/foo.js";code-block.js:1:18 lint/nursery/noPathConcat ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This file path is built by joining strings.
> 1 │ const fullPath = __dirname + "/foo.js";
│ ^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ The character that separates folders depends on the operating system, and joining strings can leave two separators in a row.
ℹ Use path.join() or path.resolve() from the node:path module instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const fullPath = `${__filename}/foo.js`;code-block.js:1:18 lint/nursery/noPathConcat ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This file path is built by joining strings.
> 1 │ const fullPath = `${__filename}/foo.js`;
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ The character that separates folders depends on the operating system, and joining strings can leave two separators in a row.
ℹ Use path.join() or path.resolve() from the node:path module instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const fullPath = import.meta.dirname + "/foo.js";code-block.js:1:18 lint/nursery/noPathConcat ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This file path is built by joining strings.
> 1 │ const fullPath = import.meta.dirname + "/foo.js";
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ The character that separates folders depends on the operating system, and joining strings can leave two separators in a row.
ℹ Use path.join() or path.resolve() from the node:path module instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import path from "node:path";const fullPath = __dirname + path.sep + "foo.js";code-block.js:2:18 lint/nursery/noPathConcat ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This file path is built by joining strings.
1 │ import path from "node:path";
> 2 │ const fullPath = __dirname + path.sep + "foo.js";
│ ^^^^^^^^^^^^^^^^^^^^
3 │
ℹ The character that separates folders depends on the operating system, and joining strings can leave two separators in a row.
ℹ Use path.join() or path.resolve() from the node:path module instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const fullUrl = import.meta.url + "/foo.js";code-block.js:1:17 lint/nursery/noPathConcat ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This URL is built by joining strings.
> 1 │ const fullUrl = import.meta.url + "/foo.js";
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ import.meta.url is the URL of the current file, so text added to its end doesn't point to a file next to it.
ℹ Use new URL() instead, for example new URL("./foo.js", import.meta.url).
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import path from "node:path";
const fullPath1 = path.join(__dirname, "foo.js");const fullPath2 = path.resolve(import.meta.dirname, "foo.js");const fullPath3 = __filename + ".map";const fullPath4 = `${__dirname}_backup`;const fullUrl = new URL("./foo.js", import.meta.url);See Also
Section titled “See Also”- If you want to disallow
__dirnameand__filenamein ES modules, seenoGlobalDirnameFilename.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.