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: any
filename
=
const fileURLToPath: any
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: any
dirname
=
const path: any
path
.
any
dirname
(
const fileURLToPath: any
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: any
dirname
=
const path: any
path
.
any
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: any
url
=
const fileURLToPath: any
fileURLToPath
(new
var URL: new (input: string | {
toString: () => string;
}, base?: string | URL) => URL
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.