sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

cannot save file ***.xls

Open FrancoisMentec opened this issue 4 years ago • 10 comments

Code:

import * as XLSX from 'xlsx/xlsx.mjs'

const filename = "issue1245";
const data = [];
data.push(['label1', 'label2']);
data.push(['data1', 'data2']);
const book = XLSX.utils.book_new();
const sheet = XLSX.utils.aoa_to_sheet(data);
XLSX.utils.book_append_sheet(book, sheet, 'sheet1');
XLSX.writeFile(book, `${filename}.xls`);

Error:

file:///C:/Users/.../node_modules/xlsx/xlsx.mjs:3220
        throw new Error("cannot save file " + fname);
              ^

Error: cannot save file issue1245.xls
    at write_dl (file:///C:/Users/.../node_modules/xlsx/xlsx.mjs:3220:8)
    at write_cfb_ctr (file:///C:/Users/.../node_modules/xlsx/xlsx.mjs:23857:23)
    at write_cfb_type (file:///C:/Users/.../node_modules/xlsx/xlsx.mjs:23905:9)
    at writeSync (file:///C:/Users/.../node_modules/xlsx/xlsx.mjs:23999:46)
    at Module.writeFileSync (file:///C:/Users/.../node_modules/xlsx/xlsx.mjs:24028:9)
    at file:///C:/Users/.../test.js:10:6
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

This is the entire code in a test.js file that I run using the command node test.js. I'm on Windows. The console I use has admin privilege, but the directory should not be protected anyway. What am I doing something wrong?

FrancoisMentec avatar Mar 29 '22 11:03 FrancoisMentec

It is import * as XLSX from 'xlsx' no idea where I found the line I used.

FrancoisMentec avatar Mar 29 '22 12:03 FrancoisMentec

same question, how to fix it ?

action-hong avatar Aug 30 '22 09:08 action-hong

@action-hong As you can see in my original code I used the line import * as XLSX from 'xlsx/xlsx.mjs' to import the library, this is the cause of the error. Instead, I should have used import * as XLSX from 'xlsx'.

FrancoisMentec avatar Aug 30 '22 09:08 FrancoisMentec

oh i miss code below

/* load 'fs' for readFile and writeFile support */
import * as fs from 'fs';
XLSX.set_fs(fs);

action-hong avatar Aug 30 '22 09:08 action-hong

@FrancoisMentec thanks for you reply

i try to use import * as XLSX from 'xlsx' but not work

According to the official documentation about esm-import

import * as XLSX from 'xlsx/xlsx.mjs';

/* load 'fs' for readFile and writeFile support */
import * as fs from 'fs';
XLSX.set_fs(fs);

/* load 'stream' for stream support */
import { Readable } from 'stream';
XLSX.stream.set_readable(Readable);

/* load the codepage support library for extended support with older formats  */
import * as cpexcel from 'xlsx/dist/cpexcel.full.mjs';
XLSX.set_cptable(cpexcel);

it works

action-hong avatar Aug 30 '22 09:08 action-hong

https://docs.sheetjs.com/docs/getting-started/installation/nodejs

In the CommonJS build, all of the Node-specific modules are loaded:

const XLSX = require("xlsx");

If you are using NodeJS with ECMAScript modules, none of the native features are loaded. You have to load them manually. The equivalent of the CommonJS require is the following:

import * as XLSX from 'xlsx/xlsx.mjs';

/* load 'fs' for readFile and writeFile support */
import * as fs from 'fs';
XLSX.set_fs(fs);

/* load 'stream' for stream support */
import { Readable } from 'stream';
XLSX.stream.set_readable(Readable);

/* load the codepage support library for extended support with older formats  */
import * as cpexcel from 'xlsx/dist/cpexcel.full.mjs';
XLSX.set_cptable(cpexcel);

@FrancoisMentec import * as XLSX from 'xlsx' most likely pulled the CommonJS build (which automatically hooks into fs)

.

Why does the ESM build require extra steps?

As it turns out, there are at least 4 conflicting implementations of ESM (Node ESM, Browser ESM, Bundler ESM, and Deno ESM for the time being) with their own quirks. Leaving an fs import would break browser ESM (since not all browsers support import maps), Webpack 5 default configuration (which got rid of node polyfills), and create-react-app (see https://github.com/facebook/create-react-app/issues/11756 for more details)

The only solution that works in all environments is to remove all imports from the library and push that integration code to userland. As @action-hong noticed, it is included in the documentation.

SheetJSDev avatar Aug 30 '22 09:08 SheetJSDev

I meet with the "cannot save file" error when added XLSX 0.18.5 module into the one big JS bundle with a help of Webpack 5. The only solution I found was to exclude it from the bundle in Webpack config by adding into externals section like that:

...
externals: { 
    xlsx: 'commonjs xlsx', // other vice module gives error when writes into a file
 }, 
...     

KEMBL avatar Apr 05 '23 14:04 KEMBL

I had the same problem. I just needed to add : XLSX.set_fs(fs), as I already had before import fs from 'fs'; So my final lines where :

import fs from 'fs';
import XLSX from "xlsx";
XLSX.set_fs(fs);
//import { Readable } from 'stream';
//XLSX.stream.set_readable(Readable);

/* load the codepage support library for extended support with older formats  */
//import * as cpexcel from 'xlsx/dist/cpexcel.full.mjs';
//XLSX.set_cptable(cpexcel);

I started with the stream and cpexcel thing, but after test, it was not needed (in my case, doing simple things).

laparn avatar Oct 20 '23 13:10 laparn

oh i miss code below

/* load 'fs' for readFile and writeFile support */
import * as fs from 'fs';
XLSX.set_fs(fs);

worked for me tnx ❤️

Syaw0 avatar Feb 24 '24 13:02 Syaw0

Just an FYI, as of today this code works on most current version:

import path from 'path';
import XLSX from 'xlsx';
import fs from 'fs';
XLSX.set_fs(fs);

//...

const datafile = path.join(baseDir, 'data.xlsx');

//...

XLSX.writeFile(workbook, path.resolve(datafile), { bookType: 'xlsx' });

I got here because I was getting the same error as OP but after looking through this post realize I just forgot the XLSX.set_fs line unlike in my other already working modules (hate when that happens, 😀). My bad - but figured I'd toss a necro here to update the earlier comments for 2024.

Versions: Node.js v20.16.0 "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",

Carnix avatar Sep 23 '24 18:09 Carnix