ObjectThe import.meta meta property is an Object that contains the following
properties. It is only supported in ES modules.
import.meta.dirname
History
stringThis is the same as the path.dirname() of the import.meta.filename.
Caveat: only present on
file:modules.
import.meta.filename
History
stringThis 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.
stringfile: 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));
booleantrue when the current module is the entry point of the current process; false otherwise.Equivalent to require.main === module in CommonJS.
Analogous to Python's __name__ == "__main__".
export function foo() { return 'Hello, world'; } function main() { const message = foo(); console.log(message); } if (import.meta.main) main(); // `foo` can be imported from another module without possible side-effects from `main`
import.meta.resolve
History
--experimental-import-meta-resolve CLI flag, except for the non-standard parentURL parameter.file: URLs that do not map to an existing file on the local FS.URL object to parentURL parameter.import.meta.resolve(specifier): string
import.meta.resolve is a module-relative resolution function scoped to
each module, returning the URL string.
const dependencyAsset = import.meta.resolve('component-lib/asset.css'); // file:///app/node_modules/component-lib/asset.css import.meta.resolve('./dep.js'); // file:///app/dep.js
All features of the Node.js module resolution are supported. Dependency resolutions are subject to the permitted exports resolutions within the package.
Caveats:
- This can result in synchronous file-system operations, which
can impact performance similarly to
require.resolve. - This feature is not available within custom loaders (it would create a deadlock).
Non-standard API:
When using the --experimental-import-meta-resolve flag, that function accepts
a second argument: