TypeError: Cozy is not a constructor

question

#1

Hi, I want to use cozy-client-js with oauth.
When I run this example code with yarn start command, it looks like this.
Is there anyone who can solve this?

TypeError: Cozy is not a constructor

22 | )
23 | }
24 |

25 | const cozy = new Cozy({
26 | cozyURL: ‘http://cozy.tools:8080’,
27 | oauth: {
28 | storage: new MemoryStorage(),

https://docs.cozy.io/en/cozy-client-js/oauth/
Complete example for Node.JS

const http = require(‘http’);
const {Cozy,MemoryStorage} = require(’./dist/cozy-client.node.js’)

function onRegistered(client, url) {
let server
return new Promise((resolve) => {
server = http.createServer((request, response) => {
if (request.url.indexOf(’/do_access’) === 0) {
console.log(‘Received access from user with url’, request.url)
resolve(request.url)
response.end()
}
})
server.listen(3333, () => {
console.log('Please visit the following url to authorize the application: ', url)
})
})
.then(
(url) => { server.close(); return url; },
(err) => { server.close(); throw err; }
)
}

const cozy = new Cozy({
cozyURL: ‘http://cozy.tools:8080’,
oauth: {
storage: new MemoryStorage(),
clientParams: {
redirectURI: ‘http://localhost:3333/do_access’,
softwareID: ‘foobar’,
clientName: ‘client’,
scopes: [‘io.cozy.files:GET’]
},
onRegistered: onRegistered,
}
})

cozy.authorize().then((creds) => console.log(creds))


#2

Hi, may I ask where you found a guide to use cozy-client-js ? We’re phasing it out and recommend using https://github.com/cozy/cozy-client instead.

As to your problem, what version of node are you using? And what happens if you console.log(Cozy)?


#3

Do you have a './dist/cozy-client.node.jsfile? I'm wondering if it should not berequire(‘cozy-client-js/dist/cozy-client.node.js’)`

Also, as said by @y_lohse we recommend to use our new lib “Cozy Client”

You can find a bit of documentation about OAuth here: https://docs.cozy.io/en/cozy-client/api/cozy-stack-client/#OAuthClient

We use it internally in our lib to do the cozy authenticate : https://github.com/cozy/cozy-libs/blob/master/packages/cozy-authentication/src/utils/onboarding.js#L97


#4

cozy-client-js comes out second when you search for cozy-client on google

node ver.: v10.16.3

I tried to write console.log (Cozy), but I can’t get it to run, since this is a transpile error.

Thank you.


#5

I will use cozy-client.

Thank you.