How to remove "_fs.readFileSync is not a function" error from cypress test

I want to read data from a particular cell of my excel sheet and then use that data in my cypress tests. The file name is ‘qaautomation.xlsx’ and sheet name is ‘Input’ and I want to read data from cell B2. I have written the following code to access the value

 /// <reference types ="cypress" />
var xlsx = require ("xlsx");
var workbook= xlsx.readFile("qaautomation.xlsx");
var worksheet= workbook.Sheets["Input"];
var cellB2value= 'B2';
var cellB2=worksheet[cellB2value];
var cellB2_value=(cellB2.v);

Code written below is where the value would be used.

        describe('Typing the address', function(){
    
    it ('should open the link', function(){
        cy.visit(cellB2_value) //here comes the value from cell B2
})})

When I run the above code I get the following error

The following error originated from your test code, not from Cypress.

  > _fs.readFileSync is not a function

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

Cypress could not associate this error to any specific test.

We dynamically generated a new test to display this failure.

Check your console for the stack trace or click this message to see where it originated from.

Is there any way to remove this error?

11 thoughts on “How to remove "_fs.readFileSync is not a function" error from cypress test”

  1. This is a problem with how the XLSX read function deals with webpack and the browser’s security feature to not access the file system directly.

    You’ll need to read the file via an ajax call and then ‘read’ it into xlsx that way. There’s a demo here:

    https://github.com/SheetJS/sheetjs/tree/master/demos/xhr

    fetch(url).then(function(res) {
      /* get the data as a Blob */
      if(!res.ok) throw new Error("fetch failed");
      return res.arrayBuffer();
    }).then(function(ab) {
      /* parse the data when it is received */
      var data = new Uint8Array(ab);
      var workbook = XLSX.read(data, {type:"array"});
    
      /* DO SOMETHING WITH workbook HERE */
    });
    
    Reply

Leave a Comment