Skip to content

dephub-js/path

Repository files navigation

@dephub/path 🗺️

Path resolution utilities for Node.js with ESM support - cwd and dirname helpers.

NPM version ESM-only


Features ✨

  • 📁 Current Working Directory - Resolve paths relative to current working directory
  • 📄 Module Directory - ESM-friendly replacement for __dirname
  • 🎯 TypeScript Ready - Full type safety with zero configuration
  • Lightweight - Minimal utilities focused on path resolution

Installation 📦

npm install @dephub/path
# or
pnpm add @dephub/path
# or
yarn add @dephub/path

Usage 🎯

Resolve Paths from Current Directory

import { cwd } from '@dephub/path';

// Get current working directory
console.log(cwd()); // /Users/user/project

// Resolve relative paths
const configPath = cwd('config', 'app.json');
// -> /Users/user/project/config/app.json

Resolve Paths from Module Directory

import { dirname } from '@dephub/path';

// In file: /project/src/utils/helpers.ts
const utilsDir = dirname(import.meta);
// -> /project/src/utils

const dataPath = dirname(import.meta, 'data.json');
// -> /project/src/utils/data.json

Real-world Example

import { cwd, dirname } from '@dephub/path';
import { readFile } from '@dephub/read';
import { writeFile } from '@dephub/write';

// Read config from project root
const config = await readFile(cwd('config.json'));

// Write output relative to current module
await writeFile(
  dirname(import.meta, 'output', 'result.json'),
  JSON.stringify(result, null, 2),
);

API Reference 📚

cwd(...paths)

Resolves paths relative to current working directory.

Parameters:

  • ...paths (string[]) - Path segments to join

Returns: string - Absolute path

dirname(meta, ...paths)

ESM replacement for __dirname - resolves paths relative to module.

Parameters:

  • meta (ImportMeta) - import.meta from calling module
  • ...paths (string[]) - Path segments to join

Returns: string - Absolute path

Throws: Error - If import.meta is not provided


License 📄

MIT License – see LICENSE for details.

Author: Estarlin R (estarlincito.com)

About

Path resolution utilities for Node.js with ESM support - cwd and dirname helpers

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors