#Can't traverse filesystem in production using `fast-glob` and `process.cwd()`

4 messages · Page 1 of 1 (latest)

wet veldt
#
import glob from 'fast-glob'
import path from 'path'

const BlogPage = async () => {
  let pages = await glob('**/*.mdx', {
    cwd: `${path.resolve()}/app/[locale]/home/blog`,
  })

  return (
    <></>
  )
}

export default BlogPage

This page lives under app/[locale]/home/blog/page.tsx, it does fetch my blog pages in development, but it DOES NOT find the pages in production. The pages array just comes empty { pages: [] } if i console.log({ pages }), but i get them in development.

silk shaleBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

jade osprey
#

Hmm, never used it but maybe the [] is throwing it off? Try logging what cwd is?

#

If it helps, I wrote this for myself

import path from "path";
import fs from "fs";

async function getMarkdownFilesRecursive(dir: string): Promise<string[]> {
  const entries = await fs.promises.readdir(dir, { withFileTypes: true });

  const filesArray = await Promise.all(
    entries.map(async (entry) => {
      const fullPath = path.join(dir, entry.name);

      if (
        entry.isFile() &&
        entry.name.startsWith("page") &&
        entry.name.endsWith(".md")
      ) {
        return [fullPath];
      }

      if (entry.isDirectory()) {
        return getMarkdownFilesRecursive(fullPath);
      }

      return [];
    }),
  );

  return filesArray.flat();
}

export async function getMarkdownAbsolutePaths() {
  const projectRoot = process.cwd();
  const contentDir = path.join(projectRoot, "content");

  const files = await getMarkdownFilesRecursive(contentDir);
  return files //.filter(onlyUnique);
}

since I wanted only any page.md under the content directory at root (i figure using nextjs conventions might be good, esp since index.md breaks sometimes and i like to bundle assets in the same directory). Might eventually add i18n with page.<lang>.md but idk yet. Potentially just using fs read dir sync is simpler amicablethink

This could be modified pretty trivially to support what you want?