We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 71f4e6b commit d864bbcCopy full SHA for d864bbc
lib/internal/modules/esm/initialize_import_meta.js
@@ -66,10 +66,20 @@ function resolveModuleMeta(url) {
66
return { __dirname: undefined, __filename: undefined };
67
}
68
69
- const filePath = fileURLToPath(url);
+ // We return an object with getters to reduce memory usage in modules that
70
+ // do not use these properties, i.e. most modules don't need the values
71
+ // to be resolved until the first time the property is accessed.
72
+ let filePath;
73
+ let dirPath;
74
return {
- __dirname: dirname(filePath),
- __filename: filePath,
75
+ get __dirname() {
76
+ return dirname(filePath ??= fileURLToPath(url));
77
+ },
78
+ get __filename() {
79
+ filePath ??= fileURLToPath(url);
80
+ dirPath ??= dirname(filePath);
81
+ return filePath;
82
83
};
84
85
0 commit comments