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.
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 ifjoin(dirPath, filename)returns the correct value?