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

find-import

v1.0.11

Published

Find and load first instance of js/json in parent directories.

Downloads

581

Readme

find-import

Find and load first instance of js/json in parent directories.

npm package License

Contents

Introduction

Load the first instance of a found module.

Optionally specify depth preference to prefer "top-most" packages.

Supports .json, .cjs, .mjs, and .js.

Returns the path and contents of the found module.

Install

npm i find-import

Example

Given file structure

/
└─┬ root
  ├── my-file.cjs // module.exports = { abc: 123 }
  └─┬ my-package
    └── my-file.json // { "foo": "bar" }
// cwd = /root/my-package
import { findImport } from 'find-import';

let found;

found = await findImport(['my-file.cjs', 'my-file.json']);
found.content // { foo: 'bar' }
found.filePath // /root/my-package/my-file.json

found = await findImport(['my-file.cjs', 'my-file.json'], {
    direction: 'down',
});
found.content // { abc: 123 }
found.filePath // /root/my-file.cjs

found = await findImport(['my-file.cjs', 'my-file.json'], {
    direction: 'down',
    startAt: '/root/my-package',
});
found.filePath // /root/my-package/my-file.json


found = await findImport(['my-file.cjs', 'my-file.json'], {
    cwd: '/root',
});
found.filePath // /root/my-package/my-file.cjs

Usage

find-import is an ESM module. That means it must be imported. To load from a CJS module, use dynamic import const { findImport } = await import('find-import');.

API

findImport(fileNames, options?)

Finds first instance of matching module, and loads. Returns file path to module and content.

Note that content is the result of a dynamic import() call. If accessing the default content, it may be necessary/convenient to extract that content. See default-import for a potential solution.

fileNames

string or array of strings

List of file names to search for in each directory.

options

  • cwd

    • Type: string or URL
    • optional, defaults to process.cwd()
    • Directory to use as base directory.
    • See parse-cwd.
  • direction

    • 'up'(default) or 'down'
    • direction to search for files.
      • 'up' indicates /foo/bar -> /foo -> /
      • 'down' is opposite, / -> /foo -> /foo/bar
  • startAt

    • Type: string or URL
    • optional, defaults to /
    • Top-most "root" directory to limit search.