Code Monkey home page Code Monkey logo

local-pkg's Introduction

local-pkg

NPM version

Get information on local packages. Works on both CJS and ESM.

Install

npm i local-pkg

Usage

import {
  getPackageInfo,
  importModule,
  isPackageExists,
  resolveModule,
} from 'local-pkg'

isPackageExists('local-pkg') // true
isPackageExists('foo') // false

await getPackageInfo('local-pkg')
/* {
 *   name: "local-pkg",
 *   version: "0.1.0",
 *   rootPath: "/path/to/node_modules/local-pkg",
 *   packageJson: {
 *     ...
 *   }
 * }
 */

// similar to `require.resolve` but works also in ESM
resolveModule('local-pkg')
// '/path/to/node_modules/local-pkg/dist/index.cjs'

// similar to `await import()` but works also in CJS
const { importModule } = await importModule('local-pkg')

Sponsors

License

MIT License © 2021 Anthony Fu

local-pkg's People

Contributors

antfu avatar chizukicn avatar sxzz avatar xeho91 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  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

local-pkg's Issues

Node 12 support

Tried using this on Node 12.16.3 but I got the engine error.
I had to use has-pkg which serves for what I want to achieve and has no engine limitation.
Is there a specific reason why the engine required is node >=14?

importModule does not work with @vitest/[email protected]

Describe the bug

I'm not sure which repo is ultimately responsible for this issue, so let me report this here.

I suddenly started running into TypeError: coverageModule.getProvider is not a function when trying to generate coverage with vitest. Let me just skip straight to the investigation results.

It seems like importModule only considers an import to be ESM if the exported object has a truthy __esModule property. Reference: https://github.com/antfu/local-pkg/blob/main/index.mjs#L20

When I look at the code and dumped the module object during debugging, the module object was found, but the default export doesn't have that property. Reference: https://unpkg.com/browse/@vitest/[email protected]/dist/index.js

After downgrading @vitest/coverage-c8 to 0.29.7 it works again.

Reproduction

I hope my explanation is enough.

System Info

System:
    OS: macOS 12.6.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 20.13 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.asdf/installs/nodejs/18.15.0/bin/node
    npm: 9.5.0 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 111.0.5563.146
    Firefox: 111.0.1
    Safari: 16.3
    Safari Technology Preview: 16.4

Used Package Manager

pnpm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Yarn PnP support

I'm trying to use Vitest and I'm running into an issue with Yarn PnP:

.pnp.cjs:101340 Uncaught Error: local-pkg tried to access vite, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: vite (via "vite/package.json")
Required by: local-pkg@npm:0.4.0 (via /Users/minh/dev/project/.yarn/cache/local-pkg-npm-0.4.0-c31827f33b-46964cea4b.zip/node_modules/local-pkg/)

I believe that this package needs to support basedir (similar to resolve), and set the paths option of require.resolve:

local-pkg/index.mjs

Lines 51 to 65 in fc9d06c

function resolvePackage(name, options = {}) {
try {
return require.resolve(`${name}/package.json`, options)
}
catch {
}
try {
return require.resolve(name, options)
}
catch (e) {
if (e.code !== 'MODULE_NOT_FOUND')
console.error(e)
return false
}
}

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.