Parses the full path to current working directory.
Validates that directory actually exists.
npm i parse-cwd
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');
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');
.
- 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