ImportMeta
[<Interface>]
type ImportMeta =
abstract member dirname: string with get, set
abstract member filename: string with get, set
abstract member main: bool with get, set
abstract member url: string with get, set
static member Create (dirname: string, filename: string, url: string, resolve: Func<string,U2<string,URL> option,string>, main: bool) : ImportMeta
abstract member resolve (specifier: string, ?parent: U2<string,URL>) : string| Assembly | Xantham.Fable.Node |
| Attributes | [<Interface>] |
Properties
abstract member dirname: string with get, set
abstract member dirname: string with get, setThe 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.
@since v21.2.0, v20.11.0
abstract member filename: string with get, set
abstract member filename: string with get, setThe 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.
@since v21.2.0, v20.11.0
abstract member main: bool with get, set
abstract member main: bool with get, settrue 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`
@since v22.18.0
abstract member url: string with get, set
abstract member url: string with get, setThe 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));
Methods
static member Create (dirname: string, filename: string, url: string, resolve: Func<string,U2<string,URL> option,string>, main: bool) : ImportMetaabstract member resolve (specifier: string, ?parent: U2<string,URL>) : string
abstract member resolve (specifier: string, ?parent: U2<string,URL>) : stringimport.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).
@since v13.9.0, v12.16.0
| Parameter | |
|---|---|
specifier | The module specifier to resolve relative to the current module. |
parent | An optional absolute parent module URL to resolve from. Default:import.meta.url |
Returns. The absolute URL string that the specifier would resolve to.