filePathsFromImportMeta
Prefer
import.meta.dirnameandimport.meta.filenameover legacy file path techniques.
✅ This rule is included in the node stylistic preset.
Node.js 20.11 introduced import.meta.dirname and import.meta.filename as modern alternatives to legacy file path techniques.
These properties provide direct access to directory and file paths without requiring imports from node:path or node:url.
import.meta.filenameis equivalent tofileURLToPath(import.meta.url).import.meta.dirnameis equivalent topath.dirname(fileURLToPath(import.meta.url))and similar legacy patterns.
Examples
Section titled “Examples”const filename = fileURLToPath(import.meta.url);const dirname = path.dirname(fileURLToPath(import.meta.url));const dirname = path.dirname(import.meta.filename);const url = fileURLToPath(new URL(".", import.meta.url));const filename = import.meta.filename;const dirname = import.meta.dirname;const url = import.meta.url;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you need to support Node.js versions earlier than 20.11, you should not enable this rule. Stylistically, if you prefer dynamically constructing URLs to be consistent, this rule might not be for you.
Further Reading
Section titled “Further Reading”- Node.js: import.meta.dirname
- Node.js: import.meta.filename
- Node.js: import.meta.url
- Node.js: ECMAScript Modules
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.