parse-cwd
v1.0.13
Published
Convert relative path or URL to full path
Downloads
757
Readme
>parse-cwd
Parse full current working directory from relative path or URL.
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 import
ed. 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
orURL
ornull
- optional, defaults to
process.cwd()
- Optionally wrap as an object, e.g.
{ cwd: '/foo/bar' }
- Convenient for directly passing higher level
options
object
- Convenient for directly passing higher level