parse-cwd
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

parse-cwd

Parse full current working directory from relative path or URL.

npm package License Quality

Contents

Introduction

Parses the full path to current working directory.

Validates that directory actually exists.

Install

npm i parse-cwd

Example

import { parseCwd } from 'parse-cwd';

console.log(process.cwd()); // /path/to/cwd
console.log(import.meta.url); // file:///path/to/cwd/foo/bar/my-file.js

console.log(await parseCwd()); // /path/to/cwd
console.log(await parseCwd(process.cwd())); // /path/to/cwd
console.log(await parseCwd('foo/bar/my-file.js')); // /path/to/cwd/foo/bar
console.log(await parseCwd('./foo/bar/my-file.js')); // /path/to/cwd/foo/bar
console.log(await parseCwd(import.meta.url)); // /path/to/cwd/foo/bar
console.log(await parseCwd(new URL(import.meta.url))); // /path/to/cwd/foo/bar
console.log(await parseCwd({ cwd: 'foo/bar/my-file.js' })); // /path/to/cwd/foo/bar

// Error - Directory not found
await parseCwd('does/not/exist');

Usage

parse-cwd is an ESM module. That means it must be imported. To load from a CJS module, use dynamic import const { parseCwd } = await import('parse-cwd');.

API

parseCwd(cwd)

  • cwd
    • file path to resolve to URL
    • Type: string or URL or null
    • optional, defaults to process.cwd()
    • Optionally wrap as an object, e.g. { cwd: '/foo/bar' }
      • Convenient for directly passing higher level options object

Readme

Keywords

Package Sidebar

Install

npm i parse-cwd

Weekly Downloads

162

Version

1.0.8

License

MIT

Unpacked Size

5.73 kB

Total Files

5

Last publish

Collaborators

  • jacobley