matrix-js-sdk: TypeError: this.opts.request is not a function when running client in Node.js; reopen issue #1191

I would like to reopen the issue #1191 since there is a similar error. I’ve installed matrix-sdk-js using Node.js v16.15.0 and initiated client as per example. Received TypeError: this.opts.request is not a function. Also tried with nodejs v12, import or require of SDK, using async ... await function, but the same issue. any ideas?

_Originally posted by @jt0in3e in https://github.com/matrix-org/matrix-js-sdk/issues/1191#issuecomment-1141020280_

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 38 (12 by maintainers)

Commits related to this issue

Most upvoted comments

You can workaround it using

const matrixcs = require("matrix-js-sdk/lib/matrix");
const request = require("request");
matrixcs.request(request);

image

any update on this issue please ? this seems really blocking: usage of the JS SDK is blocked by this error .

But you’re still using the requests package, which doesn’t really help the rest of things out. This package has been deprecated for about 2 years already - this sdk really needs to move away from it altogether.

Hi, I had the same issue.

Setting ICreateClientOpts.request did the trick for me.

` const request = require(“request”);

// …

const client = sdk.createClient({ baseUrl: ‘https://matrix.org’, request: request })

// … `

Why is this issue closed? A workaround is not a fix, this issue should stay open until a real fix is merged and validated.

accidentally clicked on “close with comment” o_0

I can’t use the SDK at the moment because of this - this needs to be top priority

It was not clear whether you’re in ts or js? Anyway, using the responses from this post I am able to get this working on node v16.15.0

test.js

import * as sdk from 'matrix-js-sdk'
import matrixcs from 'matrix-js-sdk'
import request from 'request'
matrixcs.request(request)

console.log('starting...')

const client = sdk.createClient('https://matrix.org')
console.log('we have a client...')

client.login('m.login.password', {
    'user': '@metaspan:matrix.org',
    'password': '123456'
})
  .then((response) => {
      console.log(response)
      console.log(response.access_token)
  })
  .catch(err => {
      console.warn('caught an error!')
      console.error(err)
  })
  .finally(() => {
      console.log('we\'re done')
  })

console.debug('all done...')

produces

node test.js
starting...
we have a client...
all done...
{
  user_id: '@metaspan:matrix.org',
  access_token: 'syt_asdfasdf_asdfasdf_3b2yjA',
  home_server: 'matrix.org',
  device_id: 'asdfJJasdf',
  well_known: { 'm.homeserver': { base_url: 'https://matrix-client.matrix.org/' } }
}
syt_asdfasdf_asdfasdf_3b2yjA
we're done

I just ran into this same error