├── fixture └── .gitkeep ├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.js ├── index.test-d.ts ├── test.js ├── license ├── package.json ├── readme.md └── index.d.ts /fixture/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import {findUp, findUpSync} from 'find-up-simple'; 3 | 4 | export async function packageUp({cwd = process.cwd()} = {}) { 5 | return findUp('package.json', {cwd}); 6 | } 7 | 8 | export function packageUpSync({cwd = process.cwd()} = {}) { 9 | return findUpSync('package.json', {cwd}); 10 | } 11 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import {packageUp, packageUpSync} from './index.js'; 3 | 4 | expectType>(packageUp()); 5 | expectType>(packageUp({cwd: '.'})); 6 | expectType(packageUpSync()); 7 | expectType(packageUpSync({cwd: '.'})); 8 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import {fileURLToPath} from 'node:url'; 3 | import test from 'ava'; 4 | import {packageUp, packageUpSync} from './index.js'; 5 | 6 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); 7 | 8 | const cwd = path.join(__dirname, 'fixture'); 9 | const packagePath = path.join(__dirname, 'package.json'); 10 | 11 | test('async', async t => { 12 | t.is(await packageUp({cwd}), packagePath); 13 | t.is(path.dirname(await packageUp()), __dirname); 14 | }); 15 | 16 | test('sync', t => { 17 | t.is(packageUpSync({cwd}), packagePath); 18 | t.is(path.dirname(packageUpSync()), __dirname); 19 | }); 20 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "package-up", 3 | "version": "5.0.0", 4 | "description": "Find the closest package.json file", 5 | "license": "MIT", 6 | "repository": "sindresorhus/package-up", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "pkg", 31 | "package", 32 | "file", 33 | "find", 34 | "up", 35 | "find-up", 36 | "findup", 37 | "look-up", 38 | "look", 39 | "search", 40 | "match", 41 | "resolve", 42 | "parent", 43 | "parents", 44 | "folder", 45 | "directory", 46 | "walk", 47 | "walking", 48 | "path" 49 | ], 50 | "dependencies": { 51 | "find-up-simple": "^1.0.0" 52 | }, 53 | "devDependencies": { 54 | "ava": "^5.3.1", 55 | "tsd": "^0.29.0", 56 | "xo": "^0.56.0" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # package-up 2 | 3 | > Find the closest package.json file 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install package-up 9 | ``` 10 | 11 | ## Usage 12 | 13 | ``` 14 | / 15 | └── Users 16 | └── sindresorhus 17 | └── foo 18 | ├── package.json 19 | └── bar 20 | ├── baz 21 | └── example.js 22 | ``` 23 | 24 | ```js 25 | // example.js 26 | import {packageUp} from 'package-up'; 27 | 28 | console.log(await packageUp()); 29 | //=> '/Users/sindresorhus/foo/package.json' 30 | ``` 31 | 32 | ## API 33 | 34 | ### packageUp(options?) 35 | 36 | Returns a `Promise` for the file path, or `Promise` if it could not be found. 37 | 38 | ### packageUpSync(options?) 39 | 40 | Returns the file path, or `undefined` if it could not be found. 41 | 42 | #### options 43 | 44 | Type: `object` 45 | 46 | #### cwd 47 | 48 | Type: `string`\ 49 | Default: `process.cwd()` 50 | 51 | The directory to start from. 52 | 53 | ## Related 54 | 55 | - [read-package-up](https://github.com/sindresorhus/read-package-up) - Read the closest package.json file 56 | - [package-directory](https://github.com/sindresorhus/package-directory) - Find the root directory of an npm package 57 | - [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories 58 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type Options = { 2 | /** 3 | The directory to start from. 4 | 5 | @default process.cwd() 6 | */ 7 | readonly cwd?: string; 8 | }; 9 | 10 | /** 11 | Find the closest `package.json` file. 12 | 13 | @returns The file path, or `undefined` if it could not be found. 14 | 15 | @example 16 | ``` 17 | // / 18 | // └── Users 19 | // └── sindresorhus 20 | // └── foo 21 | // ├── package.json 22 | // └── bar 23 | // ├── baz 24 | // └── example.js 25 | 26 | // example.js 27 | import {packageUp} from 'package-up'; 28 | 29 | console.log(await packageUp()); 30 | //=> '/Users/sindresorhus/foo/package.json' 31 | ``` 32 | */ 33 | export function packageUp(options?: Options): Promise; 34 | 35 | /** 36 | Synchronously find the closest `package.json` file. 37 | 38 | @returns The file path, or `undefined` if it could not be found. 39 | 40 | @example 41 | ``` 42 | // / 43 | // └── Users 44 | // └── sindresorhus 45 | // └── foo 46 | // ├── package.json 47 | // └── bar 48 | // ├── baz 49 | // └── example.js 50 | 51 | // example.js 52 | import {packageUpSync} from 'package-up'; 53 | 54 | console.log(packageUpSync()); 55 | //=> '/Users/sindresorhus/foo/package.json' 56 | ``` 57 | */ 58 | export function packageUpSync(options?: Options): string | undefined; 59 | 60 | --------------------------------------------------------------------------------