resolve-pkg
Resolve the path of a package regardless of it having an entry point
Last updated 6 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install resolve-pkg 
SYNC missed versions from official npm registry.

resolve-pkg Build Status

Resolve the path of a package regardless of it having an entry point

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.

Install

$ npm install resolve-pkg

Usage

const resolvePkg = require('resolve-pkg');

// $ npm install --save-dev grunt-svgmin

resolvePkg('grunt-svgmin/tasks', {cwd: __dirname});
//=> '/Users/sindresorhus/unicorn/node_modules/grunt-svgmin/tasks'

// Fails here as Grunt tasks usually don't have a defined main entry point
require.resolve('grunt-svgmin/tasks');
//=> Error: Cannot find module 'grunt-svgmin'

API

resolvePkg(moduleId, [options])

moduleId

Type: string

What you would use in require().

options

cwd

Type: string
Default: process.cwd()

Directory to resolve from.

Related

  • resolve-cwd - Resolve the path of a module from the current working directory
  • resolve-from - Resolve the path of a module from a given path
  • resolve-global - Resolve the path of a globally installed module
  • import-from - Import a module from a given path
  • import-cwd - Import a module from the current working directory
  • import-lazy - Import a module lazily

License

MIT © Sindre Sorhus

Current Tags

  • 2.0.0                                ...           latest (6 months ago)

4 Versions

  • 2.0.0                                ...           6 months ago
  • 1.0.0                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 107
This Week 1,207
This Month 2,815
Last Day 239
Last Week 1,352
Last Month 6,065
Dependencies (1)
Dev Dependencies (8)
Dependents (94)

Copyright 2014 - 2016 © taobao.org |