Socket
Socket
Sign inDemoInstall

pkg-dir

Package Overview
Dependencies
6
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

pkg-dir

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


Version published
Maintainers
1
Weekly downloads
56,629,677
decreased by-7.55%

Weekly downloads

Package description

What is pkg-dir?

The pkg-dir npm package is designed to help developers find the root directory of a Node.js project or npm package. This is particularly useful in situations where your code needs to access project-wide resources or configurations, and you need a reliable way to locate the root directory of the project, regardless of the current working directory of the process.

What are pkg-dir's main functionalities?

Find the root directory of a project

This feature allows you to asynchronously find the root directory of a project by passing the current directory to the function. It returns a promise that resolves with the path to the nearest package directory.

const pkgDir = require('pkg-dir');

(async () => {
  const rootDir = await pkgDir(__dirname);
  console.log(rootDir);
})();

Synchronous finding of the project root directory

For scenarios where asynchronous code is not ideal, pkg-dir provides a synchronous method to find the project root directory. This method is useful in synchronous scripts or module initialization.

const pkgDir = require('pkg-dir');

const rootDir = pkgDir.sync(__dirname);
console.log(rootDir);

Other packages similar to pkg-dir

Readme

Source

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 undefined if it couldn't be found.

pkgDir.sync([cwd])

Returns the project root path or undefined.

cwd

Type: string
Default: process.cwd()

Directory to start from.

  • 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

Keywords

FAQs

Last updated on 12 Mar 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc