Getting TypeError: Client is not a constructor
Describe the bug When trying to initialize the client like this:
import Client from "nextcloud-node-client";
const client = new Client();
I get:
file:///C:/Users/ilja/Desktop/Projects/Development/Other/Plugins/steam-cloud-sync/nct.js:3
const client = new Client();
^
TypeError: Client is not a constructor
at file:///C:/Users/ilja/Desktop/Projects/Development/Other/Plugins/steam-cloud-sync/nct.js:3:16
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)
To Reproduce
- Make a new JS/TS file (with the latest node version)
- Import and initialize the client constructor
Expected behavior Client should get initialized without any trouble
Additional context Tried it with various TS configs and JS EMS syntax
I would strongly recommend to switch to TypeScript. However, import and require with defaults is a mess...
Remove the typings in the example code and use this line to get the client
const Client = require("nextcloud-node-client").Client;
Find the js example example below. Regards Holger
// tslint:disable:no-console
// get files recursively
const Client = require("nextcloud-node-client").Client;
(async () => {
const client = new Client();
const sourceFolder = await client.getFolder("/Borstenson/Company Information");
if (!sourceFolder) {
console.log("source folder not found");
process.exit(1);
}
// only pdfs and jpg should be listed
const fileFilterFunction = (file) => {
if (file.mime === "application/pdf" || file.mime === "image/jpeg") {
return file;
}
return null;
}
const options = {
sourceFolder,
filterFile: fileFilterFunction,
};
const command = new GetFilesRecursivelyCommand(client, options);
// get files asynchronously (will not throw exceptions!)
command.execute();
// check the processing status as long as the command is running
while (command.isFinished() !== true) {
// wait one second
await (async () => { return new Promise(resolve => setTimeout(resolve, 1000)) })();
console.log(command.getPercentCompleted() + "%");
}
// use the result to do the needful
const commandResult = command.getResultMetaData();
if (command.getStatus() === CommandStatus.success) {
console.log(commandResult.messages);
for (const file of command.getFiles()) {
console.log(file.name);
}
} else {
console.log(commandResult.errors);
}
})();
Ok. So I've tried your example with es modules. It works, but there's a weird thing I had to do to get it working.
I had to add .default to the Client class when constructing it.
What my script looks like now:
import Client from 'nextcloud-node-client';
console.log(Client.default);
(async () => {
const client = new Client.default();
console.log('Should work without any errors`);
})();
You maybe should put that in your documentation. But thanks for your help anyways. If you find a better way to do it with esm: tell me!