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

recursive-directory

v1.2.3

Published

Recursive Directory is a package that allows you to recurse a directory, you can return multiple choices as an object, list, or tree.

Downloads

25

Readme

Installation

Node/npm

#npm
npm install --save recursive-directory

#yarn
yarn add recursive-directory

#pnpm
pnpm add recursive-directory

Deno

Unlike Node, Deno doesn't use a package management like NPM and instead depends on direct URL imports. You can access recursive-directory on deno.land/x. This is how the most recent version may be imported:

You can also specify a particular version:

import { recursiveDirectory } from 'https://deno.land/x/[email protected]/mod.ts';

or letest version:

import { recursiveDirectory } from 'https://deno.land/x/recursive_directory/mod.ts';

CDN

<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/recursive-directory/lib/index.umd.js"></script>

<!-- Unpkg -->
<script src="https://unpkg.com/recursive-directory/lib/index.umd.js"></script>

NOTE: There isn't much of a change in how it's used, but the remainder of this README assumes you're using npm and importing straight from the recursive-directory.

Demo

List of files

  • TypeScript
import { Files, recursiveDirectory } from 'recursive-directory';

(async () => {
  const files: Files = (await recursiveDirectory('./dir')) as Files;

  console.log(files);
})();
  • ECMAScript modules
import { recursiveDirectory } from 'recursive-directory';

(async () => {
  const files = await recursiveDirectory('./dir');

  console.log(files);
})();
  • CommonJS
const { recursiveDirectory } = require('recursive-directory');

(async () => {
  const files = await recursiveDirectory('./dir');

  console.log(files);
})();
  • Expected result
[
  "/home/mkabumttar/work/recursive-directory/dir/assets/css/normalize.css",
  "/home/mkabumttar/work/recursive-directory/dir/assets/css/style.css",
  "/home/mkabumttar/work/recursive-directory/dir/assets/icons/favicon.ico",
  "/home/mkabumttar/work/recursive-directory/dir/assets/icons/icon.svg",
  "/home/mkabumttar/work/recursive-directory/dir/assets/images/logo.png",
  "/home/mkabumttar/work/recursive-directory/dir/assets/images/logo.svg",
  "/home/mkabumttar/work/recursive-directory/dir/assets/js/main.js",
  "/home/mkabumttar/work/recursive-directory/dir/index.html",
  "/home/mkabumttar/work/recursive-directory/dir/manifest.webmanifest"
]

List of files as object

  • TypeScript
import { RecursiveDirectory, recursiveDirectory } from 'recursive-directory';

(async () => {
  const files: RecursiveDirectory = (await recursiveDirectory(
    './dir',
    true,
  )) as RecursiveDirectory;

  console.log(files);
})();
  • ECMAScript modules
import { recursiveDirectory } from 'recursive-directory';

(async () => {
  const files = await recursiveDirectory('./dir', true);

  console.log(files);
})();
  • CommonJS
const { recursiveDirectory } = require('recursive-directory');

(async () => {
  const files = await recursiveDirectory('./dir', true);

  console.log(files);
})();
  • Expected result
[
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/css/normalize.css",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/css/",
    "filename": "normalize.css",
    "dirname": "css"
  },
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/css/style.css",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/css/",
    "filename": "style.css",
    "dirname": "css"
  },
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/icons/favicon.ico",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/icons/",
    "filename": "favicon.ico",
    "dirname": "icons"
  },
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/icons/icon.svg",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/icons/",
    "filename": "icon.svg",
    "dirname": "icons"
  },
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/images/logo.png",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/images/",
    "filename": "logo.png",
    "dirname": "images"
  },
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/images/logo.svg",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/images/",
    "filename": "logo.svg",
    "dirname": "images"
  },
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/js/main.js",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/js/",
    "filename": "main.js",
    "dirname": "js"
  },
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/index.html",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/",
    "filename": "index.html",
    "dirname": "dir"
  },
  {
    "fullpath": "/home/mkabumttar/work/recursive-directory/dir/manifest.webmanifest",
    "filepath": "/home/mkabumttar/work/recursive-directory/dir/",
    "filename": "manifest.webmanifest",
    "dirname": "dir"
  }
]

List of files as tree

  • TypeScript
import { directoryTree, Files, recursiveDirectory } from 'recursive-directory';

(async () => {
  const files: Files = (await recursiveDirectory('./dir')) as Files;

  const fileTree = directoryTree(files);

  console.log(fileTree);
})();
  • ECMAScript modules
import { recursiveDirectory, directoryTree } from 'recursive-directory';

(async () => {
  const files = await recursiveDirectory('./dir');

  const fileTree = directoryTree(files);

  console.log(fileTree);
})();
  • CommonJS
const { recursiveDirectory, directoryTree } = require('recursive-directory');

(async () => {
  const files = await recursiveDirectory('./dir');

  const fileTree = directoryTree(files);

  console.log(fileTree);
})();
  • Expected result
/home/mkabumttar/work/recursive-directory/dir
├── assets
|  ├── css
|  |  ├── normalize.css
|  |  └── style.css
|  ├── icons
|  |  ├── favicon.ico
|  |  └── icon.svg
|  ├── images
|  |  ├── logo.png
|  |  └── logo.svg
|  └── js/main.js
├── index.html
└── manifest.webmanifest

Example

  • TypeScript
import { Files, recursiveDirectory } from 'recursive-directory';
import fs from 'fs';
import path from 'path';

(async () => {
  const files: Files = (await recursiveDirectory('./dir')) as Files;

  fs.writeFileSync(
    path.resolve(__dirname, 'build.config.json'),
    JSON.stringify(files),
  );
})();
import { RecursiveDirectory, recursiveDirectory } from 'recursive-directory';
import fs from 'fs';
import path from 'path';

(async () => {
  const files: RecursiveDirectory = (await recursiveDirectory(
    './dir',
    true,
  )) as RecursiveDirectory;

  fs.writeFileSync(
    path.resolve(__dirname, 'build.config.json'),
    JSON.stringify(files),
  );
})();
import { directoryTree, Files, recursiveDirectory } from 'recursive-directory';
import fs from 'fs';
import path from 'path';

(async () => {
  const files: Files = (await recursiveDirectory('./dir')) as Files;

  const fileTree = directoryTree(files);

  fs.writeFileSync(path.resolve(__dirname, 'build.config.txt'), fileTree);
})();
  • ECMAScript modules
import { recursiveDirectory } from 'recursive-directory';
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';

(async () => {
  const __filename = fileURLToPath(import.meta.url);
  const __dirname = dirname(__filename);

  const files = await recursiveDirectory('./dir');

  fs.writeFileSync(
    path.resolve(__dirname, 'build.config.json'),
    JSON.stringify(files),
  );
})();
import { recursiveDirectory } from 'recursive-directory';
import path, { dirname } from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';

(async () => {
  const __filename = fileURLToPath(import.meta.url);
  const __dirname = dirname(__filename);

  const files = await recursiveDirectory('./dir', true);

  fs.writeFileSync(
    path.resolve(__dirname, 'build.config.json'),
    JSON.stringify(files),
  );
})();
import { recursiveDirectory, directoryTree } from 'recursive-directory';
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';

(async () => {
  const __filename = fileURLToPath(import.meta.url);
  const __dirname = dirname(__filename);

  const files = await recursiveDirectory('./dir');

  const fileTree = directoryTree(files);

  fs.writeFileSync(path.resolve(__dirname, 'build.config.txt'), fileTree);
})();
  • CommonJS
const { recursiveDirectory } = require('recursive-directory');
const path = require('path');
const fs = require('fs');

(async () => {
  const files = await recursiveDirectory('./dir');

  fs.writeFileSync(
    path.resolve(__dirname, 'build.config.json'),
    JSON.stringify(files),
  );
})();
const { recursiveDirectory } = require('recursive-directory');
const path = require('path');
const fs = require('fs');

(async () => {
  const files = await recursiveDirectory('./dir', true);

  fs.writeFileSync(
    path.resolve(__dirname, 'build.config.json'),
    JSON.stringify(files),
  );
})();
const { recursiveDirectory, directoryTree } = require('recursive-directory');
const path = require('path');
const fs = require('fs');

(async () => {
  const files = await recursiveDirectory('./dir');

  const fileTree = directoryTree(files);

  fs.writeFileSync(path.resolve(__dirname, 'build.config.txt'), fileTree);
})();

NOTE: In TypeScript may be you get an errror:

Cannot find module 'recursive-directory'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?ts(2792)

This error message tells you the typescript default module resolution is classic, this would not search from node_modules. That why tell you did not find the module in the node_modules folder. You need to change in tsconfig.json

  • tsconfig.json
{
  "compilerOptions": {
    "moduleResolution": "node",
    ...
  }
}