esm-module-paths
Advanced tools
Comparing version 1.0.1 to 1.1.0
export declare function getFilename(): string; | ||
export declare function getDirname(): string; | ||
export declare function getModulePaths(): { | ||
__filename: string; | ||
__dirname: string; | ||
}; | ||
export declare const __filename: typeof getFilename; | ||
export declare const __dirname: typeof getDirname; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -12,4 +12,4 @@ import { fileURLToPath } from "url"; | ||
var _a; | ||
var fileName = (_a = callsites()[1]) === null || _a === void 0 ? void 0 : _a.getFileName(); | ||
return extractFilePathFromFileUrl(fileName); | ||
var url = (_a = callsites()[1]) === null || _a === void 0 ? void 0 : _a.getFileName(); | ||
return extractFilePathFromFileUrl(url); | ||
} | ||
@@ -19,6 +19,15 @@ export function getDirname() { | ||
// Can't be reused from getFilename because that would change the callstack and make [1] not what we need. | ||
var fileName = (_a = callsites()[1]) === null || _a === void 0 ? void 0 : _a.getFileName(); | ||
return dirname(extractFilePathFromFileUrl(fileName)); | ||
var url = (_a = callsites()[1]) === null || _a === void 0 ? void 0 : _a.getFileName(); | ||
return dirname(extractFilePathFromFileUrl(url)); | ||
} | ||
export function getModulePaths() { | ||
var _a; | ||
var url = (_a = callsites()[1]) === null || _a === void 0 ? void 0 : _a.getFileName(); | ||
var fileName = extractFilePathFromFileUrl(url); | ||
return { | ||
__filename: fileName, | ||
__dirname: dirname(fileName), | ||
}; | ||
} | ||
export var __filename = getFilename; | ||
export var __dirname = getDirname; |
{ | ||
"name": "esm-module-paths", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Returns dirname and filename for ESM modules in node", | ||
@@ -5,0 +5,0 @@ "exports": { |
# esm-module-paths | ||
ESM modules don't allow to use [`__dirname`](https://nodejs.org/docs/latest/api/modules.html#modules_dirname) and [`__filename`](https://nodejs.org/docs/latest/api/modules.html#modules_filename) do determine a `.js` file's location. | ||
Instead, you need to copy/paste [multiple lines](https://stackoverflow.com/a/50052194) each time to achieve the same. | ||
Instead, you need to copy/paste [multiple lines and expressions](https://stackoverflow.com/a/50052194) each time to achieve the same. | ||
@@ -13,3 +13,15 @@ This package makes this a bit easier. | ||
```js | ||
import { __dirname, __filename } from "esm-module-paths"; | ||
import { getModulePaths } from "esm-module-paths"; | ||
const { __dirname, __filename } = getModulePaths(); | ||
console.log(__dirname); // /your/path | ||
console.log(__filename); // /your/path/index.js | ||
``` | ||
For convenience reasons, the library also exports `__filename` and `__dirname` as functions, which can be used independently. | ||
```js | ||
import { __filename, __dirname } from "esm-module-paths"; | ||
console.log(__dirname()); // /your/path | ||
@@ -22,3 +34,6 @@ console.log(__filename()); // /your/path/index.js | ||
```js | ||
import {getFileName, getDirName} | ||
import { getFileName, getDirName } from "esm-module-paths"; | ||
console.log(getDirName()); // /your/path | ||
console.log(getFileName()); // /your/path/index.js | ||
``` | ||
@@ -28,3 +43,3 @@ | ||
``` | ||
```sh | ||
npm install esm-module-paths | ||
@@ -35,4 +50,4 @@ ``` | ||
``` | ||
```sh | ||
yarn add esm-module-paths | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3743
39
50