Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fs throw error not a direcectory when path is good

This is how function is looking and error code. Can someone see what I’m doing wrong?
Text

Here is code:

for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync(`./src/functions/${folder}`)
    .filter((file) => file.endsWith('.js'));

  for (const file of functionFiles) {
    await import(`./src/functions/${folder}/${file}`);
  }
}

and error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

node:fs:1451
  handleErrorFromBinding(ctx);
  ^

Error: ENOTDIR: not a directory, scandir './src/functions/functions.js'
    at Object.readdirSync (node:fs:1451:3)
    at file:///C:/Users/rafal/OneDrive/Pulpit/VeriusMusic/src/index.js:22:6
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  errno: -4052,
  syscall: 'scandir',
  code: 'ENOTDIR',
  path: './src/functions/functions.js'
}```

>Solution :

readdirSync only works with the directories, which means you can not give a path of file.

You need to pass withFileTypes to the first readdirSync and filter all directories:

const functionFolders = readdirSync(`./src/functions`, { withFileTypes: true })
  .filter((dirent) => dirent.isDirectory());

Now functionFolders contains only the path of directories, not files.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading