├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── fixtures └── private-module-test │ └── package.json ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.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 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/resolve-pkg 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.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 | - 14 14 | - 12 15 | - 10 16 | - 8 17 | steps: 18 | - uses: actions/checkout@v2 19 | - uses: actions/setup-node@v1 20 | with: 21 | node-version: ${{ matrix.node-version }} 22 | - run: npm install 23 | - run: npm test 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /fixtures/private-module-test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@someprivate/module-test", 3 | "version": "0.0.0" 4 | } 5 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare namespace resolvePkg { 2 | interface Options { 3 | /** 4 | Directory to resolve from. 5 | 6 | @default process.cwd() 7 | */ 8 | readonly cwd?: string; 9 | } 10 | } 11 | 12 | /** 13 | Resolve the path of a package regardless of it having an entry point. 14 | 15 | @param moduleId - What you would use in `require()`. 16 | 17 | @example 18 | ``` 19 | import resolvePkg = require('resolve-pkg'); 20 | 21 | // $ npm install --save-dev grunt-svgmin 22 | 23 | resolvePkg('grunt-svgmin/tasks', {cwd: __dirname}); 24 | //=> '/Users/sindresorhus/unicorn/node_modules/grunt-svgmin/tasks' 25 | 26 | // Fails here as Grunt tasks usually don't have a defined main entry point 27 | require.resolve('grunt-svgmin/tasks'); 28 | //=> Error: Cannot find module 'grunt-svgmin' 29 | ``` 30 | */ 31 | declare function resolvePkg( 32 | moduleId: string, 33 | options?: resolvePkg.Options 34 | ): string | undefined; 35 | 36 | export = resolvePkg; 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const path = require('path'); 3 | const resolveFrom = require('resolve-from'); 4 | 5 | module.exports = (moduleId, options = {}) => { 6 | const parts = moduleId.replace(/\\/g, '/').split('/'); 7 | let packageName = ''; 8 | 9 | // Handle scoped package name 10 | if (parts.length > 0 && parts[0][0] === '@') { 11 | packageName += parts.shift() + '/'; 12 | } 13 | 14 | packageName += parts.shift(); 15 | 16 | const packageJson = path.join(packageName, 'package.json'); 17 | const resolved = resolveFrom.silent(options.cwd || process.cwd(), packageJson); 18 | 19 | if (!resolved) { 20 | return; 21 | } 22 | 23 | return path.join(path.dirname(resolved), parts.join('/')); 24 | }; 25 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import resolvePkg = require('.'); 3 | 4 | expectType(resolvePkg('hello')); 5 | expectType(resolvePkg('hello', {cwd: '.'})); 6 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (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": "resolve-pkg", 3 | "version": "2.0.0", 4 | "description": "Resolve the path of a package regardless of it having an entry point", 5 | "license": "MIT", 6 | "repository": "sindresorhus/resolve-pkg", 7 | "author": { 8 | "name": "Sindre Sorhus", 9 | "email": "sindresorhus@gmail.com", 10 | "url": "sindresorhus.com" 11 | }, 12 | "engines": { 13 | "node": ">=8" 14 | }, 15 | "scripts": { 16 | "pretest": "del node_modules/@someprivate/module-test && make-dir node_modules/@someprivate/module-test && cpy 'fixtures/private-module-test/*' node_modules/@someprivate/module-test/", 17 | "test": "xo && ava && tsd" 18 | }, 19 | "files": [ 20 | "index.js", 21 | "index.d.ts" 22 | ], 23 | "keywords": [ 24 | "require", 25 | "resolve", 26 | "path", 27 | "module", 28 | "from", 29 | "like", 30 | "path", 31 | "cwd", 32 | "current", 33 | "working", 34 | "directory", 35 | "grunt", 36 | "main", 37 | "entry", 38 | "point" 39 | ], 40 | "dependencies": { 41 | "resolve-from": "^5.0.0" 42 | }, 43 | "devDependencies": { 44 | "@someprivate/module-test": "file:./fixtures/private-module-test", 45 | "ava": "^1.4.1", 46 | "cpy-cli": "^2.0.0", 47 | "del-cli": "^1.1.0", 48 | "grunt-svgmin": "^6.0.0", 49 | "make-dir-cli": "^2.0.0", 50 | "tsd": "^0.7.2", 51 | "xo": "^0.24.0" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # resolve-pkg 2 | 3 | > Resolve the path of a package regardless of it having an entry point 4 | 5 | Some packages like CLI tools and grunt tasks don't have a entry point, like `"main": "foo.js"` in package.json, resulting in them not being resolvable by `require.resolve()`. Unlike `require.resolve()`, this module also resolves packages without an entry point, returns `undefined` instead of throwing when the module can't be found, and resolves from `process.cwd()` instead `__dirname` by default. 6 | 7 | 8 | ## Install 9 | 10 | ``` 11 | $ npm install resolve-pkg 12 | ``` 13 | 14 | 15 | ## Usage 16 | 17 | ```js 18 | const resolvePkg = require('resolve-pkg'); 19 | 20 | // $ npm install --save-dev grunt-svgmin 21 | 22 | resolvePkg('grunt-svgmin/tasks', {cwd: __dirname}); 23 | //=> '/Users/sindresorhus/unicorn/node_modules/grunt-svgmin/tasks' 24 | 25 | // Fails here as Grunt tasks usually don't have a defined main entry point 26 | require.resolve('grunt-svgmin/tasks'); 27 | //=> Error: Cannot find module 'grunt-svgmin' 28 | ``` 29 | 30 | 31 | ## API 32 | 33 | ### resolvePkg(moduleId, [options]) 34 | 35 | #### moduleId 36 | 37 | Type: `string` 38 | 39 | What you would use in `require()`. 40 | 41 | #### options 42 | 43 | ##### cwd 44 | 45 | Type: `string`
46 | Default: `process.cwd()` 47 | 48 | Directory to resolve from. 49 | 50 | 51 | ## Related 52 | 53 | - [resolve-cwd](https://github.com/sindresorhus/resolve-cwd) - Resolve the path of a module from the current working directory 54 | - [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path 55 | - [resolve-global](https://github.com/sindresorhus/resolve-global) - Resolve the path of a globally installed module 56 | - [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path 57 | - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory 58 | - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily 59 | 60 | 61 | ## License 62 | 63 | MIT © [Sindre Sorhus](https://sindresorhus.com) 64 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import test from 'ava'; 3 | import resolvePkg from '.'; 4 | 5 | test('main', t => { 6 | t.is(resolvePkg('nonexistent'), undefined); 7 | t.is(resolvePkg('nonexistent/foo'), undefined); 8 | t.is(path.relative('.', resolvePkg('grunt-svgmin')), 'node_modules/grunt-svgmin'); 9 | t.is(path.relative('.', resolvePkg('grunt-svgmin/tasks')), 'node_modules/grunt-svgmin/tasks'); 10 | t.is(path.relative('.', resolvePkg('@someprivate/module-test')), 'node_modules/@someprivate/module-test'); 11 | t.is(path.relative('.', resolvePkg('@someprivate/module-test/subdir')), 'node_modules/@someprivate/module-test/subdir'); 12 | }); 13 | --------------------------------------------------------------------------------