Package Exports
- @rigor789/resolve-package-path
- @rigor789/resolve-package-path/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rigor789/resolve-package-path) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@rigor789/resolve-package-path
Small utility (no external dependencies) for finding the path of a package across different node versions.
npm i --save @rigor789/resolve-package-path
# or
yarn add @rigor789/resolve-package-path
# or
pnpm add @rigor789/resolve-package-pathThe reason this exists is because require.resolve("<packageName>/package.json") can fail if a package defines exports in their package.json and does not include package.json.
Some details on the issue can be found here: https://github.com/nodejs/node/issues/33460
This package works around the issue by:
- first trying a regular
require.resolve("<packageName>/package.json") - then falling back to
require.resolve("<packageName>")and finding the last index ofnode_modules/<packageName> - finally falls back to the same
require.resolve("<packageName>")and a filesystem traversal upwards until a matching package.json is found - if the package is not found, returns
false
API
resolvePackagePath(packageName: string, options?)
Resolves the package path. Options accepts anything that require.resovle("", options) accepts. See Node Docs
Example:
const { resolvePackagePath } = require("@rigor789/resolve-package-path");
console.log(resolvePackagePath('package-a')) // /path/to/package-a
console.log(resolvePackagePath('@scoped/package-a')) // /path/to/@scoped/package-aresolvePackageJSONPath(packageName: string, options?)
Same as resolvePackagePath but returns the path to the package.json instead.
Example:
const { resolvePackageJSONPath } = require("@rigor789/resolve-package-path");
console.log(resolvePackageJSONPath('package-a')) // /path/to/package-a/package.json
console.log(resolvePackageJSONPath('@scoped/package-a')) // /path/to/@scoped/package-a/package.jsonLicense
MIT