npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fpj

v1.3.1

Published

Resolves The Location Of The Package.Json File For The Given Dependency By Traversing The File System Up Starting From Given Path.

Downloads

233

Readme

fpj: Find Package Json

npm version

fpj Resolves The Location Of The Package.Json File For The Given Dependency By Traversing The File System Up Starting From Given Path.

yarn add -E fpj

Table Of Contents

API

The package is available by importing its default function:

import fpj from 'fpj'

async fpj(  dirname: string,  packageName: string,  opts?: FPJConfig,): FPJReturn

Returns the resolved entry point to the package. It will start checking for the presence of packages using Node's algorithm by resolving the node_modules folder first inside of the given dirname, then if not found, traverse up and repeat, until root of the OS is reached.

The preference of the entry output will be given to the module field specified in the package.json. If the main is found instead, it will be indicated with hasMain property on the returned object.

_fpj.Config: The options for fpj.

| Name | Type | Description | Default | | ------ | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | | fields | !Array<string> | Any additional fields from package.json file to return. | - | | soft | boolean | If the entry export (main or module) does not exist, soft mode will not throw an error, but add the hasEntry property to the output set to false. | false |

For example, the package.json files and meta information for 2 packages can be fetched using the following example:

/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'

(async () => {
  const zoroaster = await fpj(
    dirname('example/example.js'),
    'zoroaster'
  )
  console.log(zoroaster)
  const read = await fpj(
    dirname('example/example.js'),
    '@wrote/read'
  )
  console.log(read)
})()

FPJ gives preference to the module field and will report it as the entry if it exists. Otherwise, the main is used with the hasMain property set to true:

{
  entry: 'node_modules\\zoroaster\\depack\\index.js',
  packageJson: 'node_modules\\zoroaster\\package.json',
  version: '4.3.0',
  packageName: 'zoroaster',
  hasMain: true
}
{
  entry: 'node_modules\\@wrote\\read\\src\\index.js',
  packageJson: 'node_modules\\@wrote\\read\\package.json',
  version: '1.0.4',
  packageName: '@wrote/read'
}

_fpj.Return: The return type of the program.

| Name | Type | Description | | ---------------- | ---------------- | ---------------------------------------------------------------------------------------- | | entry* | string | The location of the package's entry file. The preference is given to the module field. | | packageJson* | string | The path to the package.json file itself. | | packageName* | string | The name of the resolved package. | | version | string | The version of the package. | | hasMain | boolean | Whether the entry is the main rather than module. | | entryExists | boolean | In soft mode, will be set to false if the entry file does not exist. |

Fields

Any additional fields from package.json that need to be present in the output can be specified in the fields property.

/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'

(async () => {
  const zoroaster = await fpj(
    dirname('example/example.js'),
    'zoroaster',
    { fields: ['license', 'bin'] },
  )
  console.log(zoroaster)
})()
{
  entry: 'node_modules\\zoroaster\\depack\\index.js',
  packageJson: 'node_modules\\zoroaster\\package.json',
  version: '4.3.0',
  packageName: 'zoroaster',
  hasMain: true,
  license: 'AGPL-3.0',
  bin: {
    zoroaster: 'depack/bin/zoroaster.js',
    'zoroaster-dev': 'src/bin/index.js'
  }
}

Soft Mode

When a package exports either a main or a module fields, fpj will check for their existence to resolve the path to the entry. When the entry does not exist, by default an error will be thrown. To disable the error, and add the entryExists: false to the output, the Soft Mode can be activated.

/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'

(async () => {
  const zoroaster = await fpj(
    dirname('example/example.js'),
    'myPackage',
    { soft: true },
  )
  console.log(zoroaster)
})()
{
  entry: 'example\\node_modules\\myPackage\\index.js',
  packageJson: 'example\\node_modules\\myPackage\\package.json',
  version: '1.0.0',
  packageName: 'myPackage',
  hasMain: true,
  entryExists: false
}

Copyright