Use the xlsx-populate library in Code node - n8n

Hello,

I am using n8n to design an automated workflow and am using a Code node to perform Excel export and import.

I am using JavaScript. The sample code is as follows:
const XlsxPopulate = require(‘xlsx-populate’);
const fs = require(‘fs’);
// Read JSON passed from the command line
const args = $input.first().json.data;
(async () => {
try {
const workbook = await XlsxPopulate.fromFileAsync(‘/scripts/Baocao.xlsx’);

    // todo
	

} catch (err) {
    console.log("An error occurred:", err);
}

})();

After running, it reports the error: “Cannot find module ‘xlsx-populate’ [line 1]” Error: Cannot find module ‘xlsx-populate’ at /usr/local/lib/node_modules/n8n/node_modules/.pnpm/@n8n+task-runner@file+packages+@[email protected]_encoding@0.1.13_zod@3.24.1_/node_modules/@n8n/task-runner/dist/js-task-runner/require-resolver.js:16:27 at VmCodeWrapper (evalmachine.:1:252) at evalmachine.:16:2 at Script.runInContext (node:vm:149:12) at runInContext (node:vm:301:6) at result (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/@n8n+task-runner@file+packages+@[email protected]_encoding@0.1.13_zod@3.24.1_/node_modules/@n8n/task-runner/dist/js-task-runner/js-task-runner.js:162:63) at new Promise () at JsTaskRunner.runForAllItems (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/@n8n+task-runner@file+packages+@[email protected]_encoding@0.1.13_zod@3.24.1_/node_modules/@n8n/task-runner/dist/js-task-runner/js-task-runner.js:156:34) at JsTaskRunner.executeTask (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/@n8n+task-runner@file+packages+@[email protected]_encoding@0.1.13_zod@3.24.1_/node_modules/@n8n/task-runner/dist/js-task-runner/js-task-runner.js:117:26) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Hi,

Did you install the library globally? (npm i -g) and set the NODE_FUNCTION_ALLOW_EXTERNAL environment variable with the name of the libary?

Reg,
J.

1 Like

Hi @jcuypers ,

Thank you for your response. Currently, I haven’t set up the variables or configured the environment. I’m using Docker to run on Kubernetes. Could you please guide me on how to set up the variables?

Thanks,

1 Like

Thanks @jcuypers support, i set env and run ok.

1 Like

Hi,

Can you please accept the answer as solution? Thanks

Reg,
J.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.