bergos / file-fetch

fetch for read and write access to the local file system
MIT License
6 stars 2 forks source link

Importing a promise generates this error "<stat>" is not exported by "__vite-browser-external", imported by "<node_modules/file-fetch/lib/get.js" #21

Open lonewolf73 opened 1 month ago

lonewolf73 commented 1 month ago

hello everyone !!

I am implementing a very simple react vite, actually at versions react@18.3.1 and vite@5.4.0. Calling file-fetch to read a local text file like :

function useReturnStdOut() {
  const [localArch,setLocalArch] = useState<string | null>(null);

  const getData = async()=>{ 
    const markdownFileContent = ( await fetch( new URL( 'file:///data/data/com.termux/files/home/test.txt', import.meta.filename ) ) );
    console.log(await markdownFileContent.text())
    setLocalArch(await markdownFileContent.text());    
  }

  useEffect(() => {
    getData();
  }, [localArch]);

  return localArch;
}

caling as represented in when I compile with npm run build I get this error :

node_modules/file-fetch/lib/get.js (2:9): "stat" is not exported by "__vite-browser-external", imported by "node_modules/file-fetch/lib/get.js".

looking at that file I see that import is called by:

import { stat } from 'node:fs/promises'

where that "stat" comes from referenced promises in the file promises.d.ts as like as :

function stat(
    path: PathLike,
    opts?: StatOptions & {
        bigint?: false | undefined;
    },
): Promise<Stats>;
function stat(
    path: PathLike,
    opts: StatOptions & {
        bigint: true;
    },
): Promise<BigIntStats>;
function stat(path: PathLike, opts?: StatOptions): Promise<Stats | BigIntStats>;

and that stat funcion is called as like as :

async function fileSize (url) {
  try {
    return (await stat(url)).size
  } catch (error) {
    return null
  }
}

and looking at that stat call I see there are 5 overloads of the Promise<Stats> .... does that error , as mentioned earlier, maybe related to those 5 overloads so it doesn't just match the right function to call? or maybe some related-dependencies? Or eventually what does mean that kind of error: ...not exported by "__vite-browser-external", imported by... ?

Thanks in advance! Cheers!

bergos commented 1 month ago

I think this is not the right library for your use case. Based on the other libraries/tools you mentioned (react & vite), I guess you are working on a Web application. This library is for Node.js backend applications. If you want to read local files from your Web application, the File System API would be the right starting point.