3

I'm building a desktop app using ElectronJS, which reads content of pre-existing XLSX files. I first created the app as a CommonJS module. Here's the minimal reproducible example code:

const path = require('node:path');
const xlsx = require('xlsx');
const dirPath = __dirname;
const filename = "ExampleXLSX.xlsx";
const workbook = xlsx.readFile(path.join(dirPath, filename)); 
if (workbook !== 'undefined') {
  console.log("Success")
}

This works.

But for other reasons, I needed to convert this module to an ES module. I now have:

import { dirname, join } from 'node:path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import * as xlsx from 'xlsx';
const dirPath = __dirname;
const filename = "ExampleXLSX.xlsx";
const workbook = xlsx.readFile(join(dirPath, filename)); 
if (workbook !== 'undefined') {
  console.log("Success")
}

This throws an error Error: Cannot access file, and gives the full (correct) path. If relevant, the path and file names have spaces in them, and I can't eliminate them.

2
  • This code contains a typo: workbook = xlsx.readFile(join(dirPath, filename);. Please createa minimal reproducible example on your computer and provide it and don't make up untested code that contains bugs. Did you check if join(dirPath, filename) returns the correct value? Commented Nov 25, 2024 at 12:45
  • Yes, I did check that that returns the correct value. It does, but you are right that I introduced a typo when removing the unnecessary fluff from the filename variable. I have therefore edited so that this is the minimal reproducible example. Commented Nov 25, 2024 at 13:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.