Skip to content

filePathsFromImportMeta

Prefer import.meta.dirname and import.meta.filename over 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.filename is equivalent to fileURLToPath(import.meta.url).
  • import.meta.dirname is equivalent to path.dirname(fileURLToPath(import.meta.url)) and similar legacy patterns.
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));

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.