{"id":17472765,"url":"https://github.com/shinnn/find-pkg-dir","last_synced_at":"2025-10-20T04:33:35.495Z","repository":{"id":57236325,"uuid":"134246101","full_name":"shinnn/find-pkg-dir","owner":"shinnn","description":"Find the root directory of a Node.js project from a given path","archived":false,"fork":false,"pushed_at":"2019-05-31T01:26:45.000Z","size":88,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-29T15:42:23.794Z","etag":null,"topics":["detection","fast","find","javascript","nodejs","optimized","package-json","path-resolution"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shinnn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-21T09:15:02.000Z","updated_at":"2022-11-08T09:30:44.000Z","dependencies_parsed_at":"2022-08-23T15:50:59.426Z","dependency_job_id":null,"html_url":"https://github.com/shinnn/find-pkg-dir","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Ffind-pkg-dir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Ffind-pkg-dir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Ffind-pkg-dir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Ffind-pkg-dir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shinnn","download_url":"https://codeload.github.com/shinnn/find-pkg-dir/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125581,"owners_count":21051768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["detection","fast","find","javascript","nodejs","optimized","package-json","path-resolution"],"created_at":"2024-10-18T17:35:52.258Z","updated_at":"2025-10-20T04:33:35.420Z","avatar_url":"https://github.com/shinnn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# find-pkg-dir\n\n[![npm version](https://img.shields.io/npm/v/find-pkg-dir.svg)](https://www.npmjs.com/package/find-pkg-dir)\n[![Build Status](https://travis-ci.com/shinnn/find-pkg-dir.svg?branch=master)](https://travis-ci.com/shinnn/find-pkg-dir)\n[![codecov](https://codecov.io/gh/shinnn/find-pkg-dir/branch/master/graph/badge.svg)](https://codecov.io/gh/shinnn/find-pkg-dir)\n\nFind the root directory of a Node.js project from a given path\n\n```javascript\nconst findPkgDir = require('find-pkg-dir');\n\n// When the /Users/shinnn/foo directory contains a package.json file:\n\nfindPkgDir('/Users/shinnn/foo'); //=\u003e '/Users/shinnn/foo'\nfindPkgDir('/Users/shinnn/foo/bar'); //=\u003e '/Users/shinnn/foo'\nfindPkgDir('/Users/shinnn/foo/bar/baz'); //=\u003e '/Users/shinnn/foo'\n```\n\nUnlike [the](https://www.npmjs.com/package/pkg-dir) [prior](https://www.npmjs.com/package/find-pkg) [arts](https://www.npmjs.com/package/find-root),\n\n* It uses [`InternalModuleStat`](https://github.com/nodejs/node/blob/v10.1.0/src/node_file.cc#L798) through [`require.resolve()`](https://nodejs.org/api/modules.html#modules_require_resolve_request_options) as it's [faster](#benchmark) than [`fs.statSync()`](https://nodejs.org/api/fs.html#fs_fs_statsync_path).\n* It checks if a path is file or directory, to avoid mistaking a `package.json` *directory* as a `package.json` file.\n* It automatically resolves symbolic links.\n\n## Installation\n\n[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).\n\n```\nnpm install find-pkg-dir\n```\n\n## API\n\n```javascript\nconst findPkgDir = require('find-pkg-dir');\n```\n\n### findPkgDir(*path*)\n\n*path*: `string` (a path to start searching from)  \nReturn: `string` (absolute path) or `null`\n\nIt finds the first directory containing a [`package.json` file](https://docs.npmjs.com/files/package.json), recursively looking up, starting with the given *path*.\n\nWhen it cannot find any `package.json` files finally, returns `null`.\n\n```javascript\nfindPkgDir('path/of/non/nodejs/project'); //=\u003e null\n```\n\n### Benchmark\n\n```\nfind-pkg-dir (this project):\nFind from the current directory          6.514971999917179 ms/op avg.\nFind from the deep directory           301.970978999976069 ms/op avg.\nResolve symlinks                         4.765490400046110 ms/op avg.\nFind from the `package.json` directory  33.653173299971968 ms/op avg.\n\nfind-pkg + path.dirname():\nFind from the current directory          7.597467000037431 ms/op avg.\nFind from the deep directory           421.827792199980479 ms/op avg.\nResolve symlinks                               N/A (operation failed)\nFind from the `package.json` directory         N/A (operation failed)\n\nfind-root:\nFind from the current directory          8.991230100020767 ms/op avg.\nFind from the deep directory           479.851285400055360 ms/op avg.\nResolve symlinks                               N/A (operation failed)\nFind from the `package.json` directory         N/A (operation failed)\n\npkg-dir:\nFind from the current directory          9.322520200069993 ms/op avg.\nFind from the deep directory           505.923578500002634 ms/op avg.\nResolve symlinks                               N/A (operation failed)\nFind from the `package.json` directory         N/A (operation failed)\n```\n\n## License\n\n[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshinnn%2Ffind-pkg-dir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshinnn%2Ffind-pkg-dir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshinnn%2Ffind-pkg-dir/lists"}