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?

Here is code:

for (const folder of functionFolders) {
  const functionFiles = fs
    .filter((file) => file.endsWith('.js'));

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

and error


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.

Leave a Reply Cancel reply