Find the root directory of a Node.js project or npm package
JavaScript
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
fixture init Sep 1, 2015
.editorconfig Meta tweaks Jun 18, 2018
.gitattributes Meta tweaks Jun 18, 2018
.gitignore Meta tweaks Jun 18, 2018
.npmrc Meta tweaks Jun 18, 2018
.travis.yml Require Node.js 6 (#2) Jun 18, 2018
index.js ES2015ify and require Node.js 4 May 4, 2017
license Meta tweaks Jun 18, 2018
package.json 3.0.0 Jun 18, 2018
readme.md Meta tweaks Jun 18, 2018
test.js ES2015ify and require Node.js 4 May 4, 2017

readme.md

pkg-dir Build Status

Find the root directory of a Node.js project or npm package

Install

$ npm install pkg-dir

Usage

/
└── Users
    └── sindresorhus
        └── foo
            ├── package.json
            └── bar
                ├── baz
                └── example.js
// example.js
const pkgDir = require('pkg-dir');

(async () => {
	const rootDir = await pkgDir(__dirname);

	console.log(rootDir);
	//=> '/Users/sindresorhus/foo'
})();

API

pkgDir([cwd])

Returns a Promise for either the project root path or null if it couldn't be found.

pkgDir.sync([cwd])

Returns the project root path or null.

cwd

Type: string
Default: process.cwd()

Directory to start from.

Related

  • pkg-dir-cli - CLI for this module
  • pkg-up - Find the closest package.json file
  • find-up - Find a file by walking up parent directories

License

MIT © Sindre Sorhus