filePathsFromImportMeta
Prefer
import.meta.dirnameandimport.meta.filenameover legacy file path techniques.
✅ This rule is included in the nodestylisticandstylisticStrictpresets.
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 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.
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));const const filename: string
filename = 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.
filename;const const dirname: 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.dirname: string
The directory name of the current module.
This is the same as the path.dirname() of the import.meta.filename.
Caveat: only present on file: modules.
dirname;const const url: string
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;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