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 and stylisticStrict presets.

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
const filename: string
filename
=
function fileURLToPath(url: string | URL, options?: FileUrlToPathOptions): string

This function ensures the correct decodings of percent-encoded characters as well as ensuring a cross-platform valid absolute path string.

import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)

@sincev10.12.0

@paramurl The file URL string or URL object to convert to a path.

fileURLToPath
(import.

The type of import.meta.

If you need to declare that a given property exists on import.meta, this type may be augmented via interface merging.

meta
.
ImportMeta.url: string

The absolute file: URL of the module.

This is defined exactly the same as it is in browsers providing the URL of the current module file.

This enables useful patterns such as relative file loading:

import { readFileSync } from 'node:fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));

url
);
const
const dirname: string
dirname
=
const path: path.PlatformPath
path
.
path.PlatformPath.dirname(path: string): string

Return the directory name of a path. Similar to the Unix dirname command.

@parampath the path to evaluate.

@throws{TypeError} if path is not a string.

dirname
(
function fileURLToPath(url: string | URL, options?: FileUrlToPathOptions): string

This function ensures the correct decodings of percent-encoded characters as well as ensuring a cross-platform valid absolute path string.

import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)

@sincev10.12.0

@paramurl The file URL string or URL object to convert to a path.

fileURLToPath
(import.

The type of import.meta.

If you need to declare that a given property exists on import.meta, this type may be augmented via interface merging.

meta
.
ImportMeta.url: string

The absolute file: URL of the module.

This is defined exactly the same as it is in browsers providing the URL of the current module file.

This enables useful patterns such as relative file loading:

import { readFileSync } from 'node:fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));

url
));
const
const dirname: string
dirname
=
const path: path.PlatformPath
path
.
path.PlatformPath.dirname(path: string): string

Return the directory name of a path. Similar to the Unix dirname command.

@parampath the path to evaluate.

@throws{TypeError} if path is not a string.

dirname
(import.

The type of import.meta.

If you need to declare that a given property exists on import.meta, this type may be augmented via interface merging.

meta
.
ImportMeta.filename: string

The full absolute path and filename of the current module, with symlinks resolved.

This is the same as the url.fileURLToPath() of the import.meta.url.

Caveat only local modules support this property. Modules not using the file: protocol will not provide it.

@sincev21.2.0, v20.11.0

filename
);
const
const url: string
url
=
function fileURLToPath(url: string | URL, options?: FileUrlToPathOptions): string

This function ensures the correct decodings of percent-encoded characters as well as ensuring a cross-platform valid absolute path string.

import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)

@sincev10.12.0

@paramurl The file URL string or URL object to convert to a path.

fileURLToPath
(new
var URL: new (url: string | URL, base?: string | URL) => URL

The URL interface is used to parse, construct, normalize, and encode URL.

MDN Reference

URL
(".", import.

The type of import.meta.

If you need to declare that a given property exists on import.meta, this type may be augmented via interface merging.

meta
.
ImportMeta.url: string

The absolute file: URL of the module.

This is defined exactly the same as it is in browsers providing the URL of the current module file.

This enables useful patterns such as relative file loading:

import { readFileSync } from 'node:fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));

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 ❤️‍🔥 around the world by the Flint team and contributors.