4

I'm trying to use ExcelJS in NestJS. For some reason, I can not open xlsx file.

exceljs uses this code for checking if file exists

  fs: {
    exists: function exists(path) {
      return new Promise(function (resolve) {
        console.log(path);
        fs.access(path, fs.constants.F_OK, function (err) {
          resolve(!err);
        });
      });
    }
  },

that code returns Cannot read property 'F_OK' of undefined. Console.log on line 4 returns path I send it correctly

However, if I try to access the same file form my service with this code

    fs.access(this.file, fs.constants.F_OK | fs.constants.W_OK, (err) => {
        if (err) {
            console.error(
                `${this.file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
        } else {
            console.log(`${this.file} exists, and it is writable`);
        }
    });

That code returns "file.xlsx exists, and it is writable". What is the difference and why I can't read the same file with exceljs?

5
  • Can you show more code around the first snippet?
    – eol
    Commented Nov 23, 2020 at 13:22
  • That code is from exceljs library you can take a look here github.com/exceljs/exceljs/blob/… Commented Nov 24, 2020 at 13:34
  • github.com/exceljs/exceljs/issues/1492
    – eol
    Commented Nov 24, 2020 at 14:17
  • Thanks for looking into it but, that's not the same issue. That issue revolves around React working in a browser that doesn't have access to nodeJS fs. Commented Nov 25, 2020 at 9:04
  • Can you show how you imported the fs library? I think it has to do with fs/promises and fs
    – Ricardo
    Commented Jun 21, 2021 at 16:52

1 Answer 1

6

I had the same problem.

The reason was because I was importing fs/promises and the constants are storaged in fs

Basically, you are trying to get the constants from a place where they were not declared.

This is how I solved:

import fsp from 'fs/promises'
import fs from 'fs'

await fsp.access(testDir, fs.constants.F_OK);

You can see that for access I use fsp.access and for the constants fs.constants.F_OK

1
  • 1
    it solved the Issue for me but the problem is also solved by require rather than import if you require('fs') then the constant is found under fs.F_OK Commented May 26, 2022 at 11:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.