Giter Site home page Giter Site logo

resolve-pkg's Introduction

resolve-pkg

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

resolve-pkg's People

Contributors

bendingbender avatar madjam002 avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

resolve-pkg's Issues

sync version?

Possible to add support for a synchronous version as resolvePkg.sync?

Does not work with package exports

Given a package that has exports in its package.json, like this:

{
    "exports": {
    ".": "./dist/index.js",
    "./*": {
      "types": "./dist/*.d.ts",
      "default": "./dist/*.js"
    }
  }
}

When trying to resolve that package, a MODULE_NOT_FOUND error is thrown with "Cannot find module '/path/to/my-package/dist/package.json.js'"

Basically the assumption here that you can always resolve the package.json of a package using node does not hold true anymore in this case. Still I'd like to be able to get the root path of that package, using node module resolution rules. ๐Ÿค”

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.