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

@wulechuan/find-package-dot-json

v0.0.7

Published

Search folder tree upwards and locate the nearest npm project root folder, returning the parsed package.json as an object.

Downloads

10

Readme

NPM Package

@wulechuan/find-package-dot-json

Introduction

Searches folder tree upwards to try to locate the nearest npm project root folder. If succeeds, returns both the found path and the parsed package.json as an object.

Usage

function ensureCWDToBeNPMProjectRootAndReturnPackageJSON(options) {


    /** **********************************************
     *   Utilizing `@wulechuan/find-package-dot-json`
     ** ********************************************** */
    const result = require('@wulechuan/find-package-dot-json')({
        /**
         * Optional.
         * Default to process.cwd()
        */
        searchingStartPath:    '<a path to start with>',
        
        /** 
         * Optional.
         * A non-string value or an empty string
         * means any npm project root folder counts,
         * ignoring the name of the npm project.
         */
        desiredNPMProjectName: '<your fancy npm project name here>',
    });
    /** ********************************************** */



    // If it fails to find one, let's throw an error here.
    if (! result) {
        throw ReferenceError('Fail to locate npm project root.');
    }


    // If the program was not thrown, now we are safe to move on.
    const {
        npmProjectRootPath,
    } = result;



    // Let's make some use of the found path.
    process.chdir(npmProjectRootPath);
    console.log(`[${
        chalk.gray(moment().format('HH:mm:ss'))
    }] Working directory changed to\n${' '.repeat('[HH:mm:ss] '.length)}${
        chalk.green(process.cwd())
    }\n\n\n`);



    // Return the result for outside world to make use of it.
    return result;
}

API

Argument

At present, there is only one argument that is accepted. And even that argument is optional.

Let's call the argument options.

options, aka arguments[0]

| Spec | Def | | ------------ | ------ | | Type | object | | Optional | yes |

{
    searchingStartPath:    '<a path to start with>',
    desiredNPMProjectName: '<your fancy npm project name here>',
}

options.searchingStartPath

| Spec | Def | | ----------------- | ------------------------------------------- | | Type | string | | Allowed Value | any valid path, either absolute or relative | | Default Value | process.cwd() | | Optional | yes |

The path of the searching starting folder.

Since the tool simply does some basic path operations to detect a package.json file, theoretically a path to a file instead of a folder as the starting point also works.

options.desiredNPMProjectName

| Spec | Def | | ----------------- | ---------------------------------------------------------------- | | Type | multiple | | Allowed Value | string that obeys npm naming rules | non-string | empty string | | Default Value | an empty string | | Optional | yes |

The desired npm project name.

  • If it takes a non-string value or an empty string, then the first matched package.json of any npm project counts.

  • If its value is a non-empty-string, then the string will be check by a Regular Expression to make sure the string to be a valid npm project name.

    • If the value is considered to be an invalid npm project name, this tool will throw an error;

    • If the value seems to be a valid one, then only the first met package.json with the matched name property counts.

    See https://docs.npmjs.com/files/package.json#name

Return Value

| Spec | Def | | ------------------ | ----------------------------- | | Type | object | | Possible Value | an object literal or a null |

  • If the tool fails to find a matched result, null is returned.

  • If the desired npm project is located successfully, an object literal is returned, which looks like:

    {
        npmProjectRootPath: 'a string',
        packageJSON:        'an object',
    }