Socket
Socket
Sign inDemoInstall

find-pkg-dir

Package Overview
Dependencies
2
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    find-pkg-dir

Find the root directory of a Node.js project from a given path


Version published
Weekly downloads
835
increased by14.23%
Maintainers
1
Install size
36.1 kB
Created
Weekly downloads
 

Readme

Source

find-pkg-dir

npm version Build Status codecov

Find the root directory of a Node.js project from a given path

const findPkgDir = require('find-pkg-dir');

// When the /Users/shinnn/foo directory contains a package.json file:

findPkgDir('/Users/shinnn/foo'); //=> '/Users/shinnn/foo'
findPkgDir('/Users/shinnn/foo/bar'); //=> '/Users/shinnn/foo'
findPkgDir('/Users/shinnn/foo/bar/baz'); //=> '/Users/shinnn/foo'

Unlike the prior arts,

  • It uses InternalModuleStat through require.resolve() as it's faster than fs.statSync().
  • It checks if a path is file or directory, to avoid mistaking a package.json directory as a package.json file.
  • It automatically resolves symbolic links.

Installation

Use npm.

npm install find-pkg-dir

API

const findPkgDir = require('find-pkg-dir');

findPkgDir(path)

path: string (a path to start searching from)
Return: string (absolute path) or null

It finds the first directory containing a package.json file, recursively looking up, starting with the given path.

When it cannot find any package.json files finally, returns null.

findPkgDir('path/of/non/nodejs/project'); //=> null

Benchmark

find-pkg-dir (this project):
Find from the current directory          6.514971999917179 ms/op avg.
Find from the deep directory           301.970978999976069 ms/op avg.
Resolve symlinks                         4.765490400046110 ms/op avg.
Find from the `package.json` directory  33.653173299971968 ms/op avg.

find-pkg + path.dirname():
Find from the current directory          7.597467000037431 ms/op avg.
Find from the deep directory           421.827792199980479 ms/op avg.
Resolve symlinks                               N/A (operation failed)
Find from the `package.json` directory         N/A (operation failed)

find-root:
Find from the current directory          8.991230100020767 ms/op avg.
Find from the deep directory           479.851285400055360 ms/op avg.
Resolve symlinks                               N/A (operation failed)
Find from the `package.json` directory         N/A (operation failed)

pkg-dir:
Find from the current directory          9.322520200069993 ms/op avg.
Find from the deep directory           505.923578500002634 ms/op avg.
Resolve symlinks                               N/A (operation failed)
Find from the `package.json` directory         N/A (operation failed)

License

ISC License © 2018 - 2019 Shinnosuke Watanabe

Keywords

FAQs

Last updated on 31 May 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