Home > Software design >  Fs throw error not a direcectory when path is good
Fs throw error not a direcectory when path is good

Time:01-18

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

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'
}```

CodePudding user response:

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.

  • Related