|
| 1 | +# [](https://discord.gg/M6aTR9eTwN) |
| 2 | + |
| 3 | +## @eliware/path [](https://www.npmjs.com/package/@eliware/path)[](LICENSE)[](https://github.com/eliware/path/actions) |
| 4 | + |
| 5 | +> An ESM/Jest/Node-friendly path utility for resolving file and directory paths in both CommonJS and ESM environments. |
| 6 | +
|
| 7 | +--- |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +- [Features](#features) |
| 12 | +- [Installation](#installation) |
| 13 | +- [Usage](#usage) |
| 14 | + - [ESM Example](#esm-example) |
| 15 | + - [CommonJS Example](#commonjs-example) |
| 16 | + - [Dynamic Import Example](#dynamic-import-example) |
| 17 | +- [API](#api) |
| 18 | +- [TypeScript](#typescript) |
| 19 | +- [License](#license) |
| 20 | + |
| 21 | +## Features |
| 22 | + |
| 23 | +- Unified API for ESM and CommonJS: pass either `import.meta` or a string (like `__dirname`) |
| 24 | +- Works seamlessly in Node.js, Jest, and modern ESM environments |
| 25 | +- TypeScript type definitions included |
| 26 | +- Simple, dependency-free, and well-tested |
| 27 | +- **NEW:** `pathUrl` helper for cross-platform dynamic import |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +```bash |
| 32 | +npm install @eliware/path |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +### ESM Example |
| 38 | + |
| 39 | +```js |
| 40 | +import path, { pathUrl } from '@eliware/path'; |
| 41 | + |
| 42 | +// for ESM, we need to pass import.meta |
| 43 | +const envFile = path(import.meta, ".env"); |
| 44 | +console.log(envFile); |
| 45 | + |
| 46 | +// Get a file URL href for dynamic import |
| 47 | +const envFileUrl = pathUrl(import.meta, ".env"); |
| 48 | +console.log(envFileUrl); |
| 49 | +// import(envFileUrl).then(mod => ...); |
| 50 | +``` |
| 51 | +
|
| 52 | +### CommonJS Example |
| 53 | +
|
| 54 | +```js |
| 55 | +const { path, pathUrl } = require('@eliware/path'); |
| 56 | + |
| 57 | +// for CommonJS, we need to pass __dirname |
| 58 | +const envFile = path(__dirname, '.env'); |
| 59 | +console.log(envFile); |
| 60 | + |
| 61 | +// Get a file URL href for dynamic import |
| 62 | +const envFileUrl = pathUrl(__dirname, '.env'); |
| 63 | +console.log(envFileUrl); |
| 64 | +// import(envFileUrl).then(mod => ...); // if using ESM loader in CJS |
| 65 | +``` |
| 66 | +
|
| 67 | +### Dynamic Import Example |
| 68 | +
|
| 69 | +```js |
| 70 | +// ESM |
| 71 | +import { pathUrl } from '@eliware/path'; |
| 72 | +const mod = await import(pathUrl(import.meta, './my-module.mjs')); |
| 73 | + |
| 74 | +// CommonJS (with ESM loader) |
| 75 | +const { pathUrl } = require('@eliware/path'); |
| 76 | +const mod = await import(pathUrl(__dirname, './my-module.mjs')); |
| 77 | +``` |
| 78 | +
|
| 79 | +## API |
| 80 | +
|
| 81 | +### `getCurrentFilename(metaOrDir?: ImportMeta | string): string` |
| 82 | +
|
| 83 | +Returns the absolute path to the current file or directory. Pass `import.meta` (ESM) or a string (e.g. `__dirname`). Throws if unavailable. |
| 84 | +
|
| 85 | +### `getCurrentDirname(metaOrDir?: ImportMeta | string, dirnameFn?: (path: string) => string): string` |
| 86 | +
|
| 87 | +Returns the absolute path to the current directory. Pass `import.meta` (ESM) or a string (e.g. `__dirname`). Throws if unavailable. |
| 88 | +
|
| 89 | +### `default path(metaOrDir: ImportMeta | string, ...segments: string[]): string` |
| 90 | +
|
| 91 | +Joins the current dirname (from `import.meta` or a string) with provided segments to form an absolute path. |
| 92 | +
|
| 93 | +### `pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string` |
| 94 | +
|
| 95 | +Returns a file URL href string for the resolved path, suitable for use with dynamic `import()` on all platforms. |
| 96 | +
|
| 97 | +## TypeScript |
| 98 | +
|
| 99 | +Type definitions are included: |
| 100 | +
|
| 101 | +```ts |
| 102 | +export function getCurrentFilename(metaOrDir?: ImportMeta | string): string; |
| 103 | +export function getCurrentDirname(metaOrDir?: ImportMeta | string, dirnameFn?: (path: string) => string): string; |
| 104 | +export const path: (metaOrDir: ImportMeta | string, ...segments: string[]) => string; |
| 105 | +export function pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string; |
| 106 | +export default path; |
| 107 | +``` |
| 108 | + |
| 109 | +## Support |
| 110 | + |
| 111 | +For help, questions, or to chat with the author and community, visit: |
| 112 | + |
| 113 | +[](https://discord.gg/M6aTR9eTwN)[](https://discord.gg/M6aTR9eTwN) |
| 114 | + |
| 115 | +**[eliware.org on Discord](https://discord.gg/M6aTR9eTwN)** |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +[MIT © 2025 Eli Sterling, eliware.org](LICENSE) |
| 120 | + |
| 121 | +## Links |
| 122 | + |
| 123 | +- [Home Page](https://eliware.org) |
| 124 | +- [GitHub](https://github.com/eliware/path) |
| 125 | +- [npm](https://www.npmjs.com/package/@eliware/path) |
| 126 | +- [Discord](https://discord.gg/M6aTR9eTwN) |
0 commit comments