matrix-js-sdk icon indicating copy to clipboard operation
matrix-js-sdk copied to clipboard

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

Open jt0in3e opened this issue 2 years ago • 38 comments

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

jt0in3e avatar May 30 '22 11:05 jt0in3e

@jt0in3e please provide an actual snippet and error

image

t3chguy avatar May 30 '22 11:05 t3chguy

@t3chguy

OS - linux (VoidLinux, 5.15.41_1) node.js -> v16.5.0 npm/v.8.5.5, yarn/v.1.22.18

steps to reproduce:

  • mkdir "matrix-node-client" && cd
  • yarn init
  • yarn add matrix-sdk-js
  • touch index.js && vim index.js code snippet:
const sdk = require("matrix-js-sdk");

const client = sdk.createClient({
  baseUrl: "https://matrix.org",
  accessToken: "st_x.x.x.x.x.x.x.x.x.x.x",
  userId: "@yyyyyyyyyyyy:matrix.org"
});

client.startClient();
  • node index.js

ERROR ->

Getting saved sync token...
Getting push rules...
Got saved sync token
Getting saved sync...
Getting push rules failed TypeError: this.opts.request is not a function
    at MatrixHttpApi.doRequest (/home/user/matrix-node-client/node_modules/matrix-js-sdk/lib/http-api.js:731:23)
    at MatrixHttpApi.requestOtherUrl (/home/user/matrix-node-client/node_modules/matrix-js-sdk/lib/http-api.js:610:17)
    at MatrixHttpApi.request (/home/user/matrix-node-client/node_modules/matrix-js-sdk/lib/http-api.js:566:17)
    at MatrixHttpApi.authedRequest (/home/user/matrix-node-client/node_modules/matrix-js-sdk/lib/http-api.js:515:33)
    at MatrixClient.getPushRules (/home/user/matrix-node-client/node_modules/matrix-js-sdk/lib/client.js:7099:22)
    at getPushRules (/home/user/matrix-node-client/node_modules/matrix-js-sdk/lib/sync.js:523:37)
    at SyncApi.sync (/home/user/matrix-node-client/node_modules/matrix-js-sdk/lib/sync.js:683:7)
    at MatrixClient.startClient (/home/user/matrix-node-client/node_modules/matrix-js-sdk/lib/client.js:470:18)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Waiting for saved sync before retrying push rules...
Got reply from saved sync, exists? false
  • running without client.startClient() and with console.log(client.http.opts.request) gives no Errors and this response (cut):
{
  get: [Function (anonymous)],
  head: [Function (anonymous)],
  options: [Function (anonymous)],
  post: [Function (anonymous)],
  put: [Function (anonymous)],
  patch: [Function (anonymous)],
  del: [Function (anonymous)],
  delete: [Function (anonymous)],
  jar: [Function (anonymous)],
  cookie: [Function (anonymous)],
  defaults: [Function (anonymous)],
  forever: [Function (anonymous)],
  Request: [Function: Request] {
    debug: undefined,
    defaultProxyHeaderWhiteList: [
      'accept',           'accept-charset',
      'accept-encoding',  'accept-language',
      'accept-ranges',    'cache-control',
      'content-encoding', 'content-language',
      'content-location', 'content-md5',
      'content-range',    'content-type',
      'connection',       'date',
      'expect',           'max-forwards',
      'pragma',           'referer',
      'te',               'user-agent',
      'via'
    ],
    defaultProxyHeaderExclusiveList: [ 'proxy-authorization' ]
  },
  initParams: [Function: initParams],
  debug: [Getter/Setter],
  default: [Function: request] {
    get: [Function (anonymous)],
    head: [Function (anonymous)],
    options: [Function (anonymous)],
    post: [Function (anonymous)],
    put: [Function (anonymous)],
    patch: [Function (anonymous)],
    del: [Function (anonymous)],
    delete: [Function (anonymous)],
    jar: [Function (anonymous)],
    cookie: [Function (anonymous)],
    defaults: [Function (anonymous)],
    forever: [Function (anonymous)],
    Request: [Function: Request] {
      debug: undefined,
      defaultProxyHeaderWhiteList: [Array],
      defaultProxyHeaderExclusiveList: [Array]
    },
    initParams: [Function: initParams],
    debug: [Getter/Setter]
  }
}

jt0in3e avatar May 30 '22 11:05 jt0in3e

https://github.com/matrix-org/matrix-js-sdk/issues/2415 fixes this

t3chguy avatar May 30 '22 12:05 t3chguy

#2415 fixes this

@t3chguy nope, doesn't work. I've tried the fix with no success. the same problem (

jt0in3e avatar May 30 '22 13:05 jt0in3e

@jt0in3e how are you testing it?

v18.0.0 image

https://github.com/matrix-org/matrix-js-sdk/pull/2414 image

t3chguy avatar May 30 '22 13:05 t3chguy

I just ran into this same error

reesericci avatar May 30 '22 14:05 reesericci

import * as sdk from "matrix-js-sdk";

const client = sdk.createClient("https://matrix.org");

client.publicRooms(function(err, data) {
  console.log("Public Rooms: %s", JSON.stringify(data));
});


client.once('sync', function(state, prevState, res) {
    if(state === 'PREPARED') {
        console.log("prepared");
    } else {
        console.log(state);
        process.exit(1);
    }
});

await client.startClient({initialSyncLimit: 10});

Node.js v17.3.0

Logs:

Public Rooms: undefined
Getting saved sync token...
Getting push rules...
Got saved sync token
Getting saved sync...
Getting push rules failed TypeError: this.opts.request is not a function
    at MatrixHttpApi.doRequest (/var/home/reesericci/js/node/internet-fax-machine/node_modules/matrix-js-sdk/lib/http-api.js:731:23)
    at MatrixHttpApi.requestOtherUrl (/var/home/reesericci/js/node/internet-fax-machine/node_modules/matrix-js-sdk/lib/http-api.js:610:17)
    at MatrixHttpApi.request (/var/home/reesericci/js/node/internet-fax-machine/node_modules/matrix-js-sdk/lib/http-api.js:566:17)
    at MatrixHttpApi.authedRequest (/var/home/reesericci/js/node/internet-fax-machine/node_modules/matrix-js-sdk/lib/http-api.js:515:33)
    at MatrixClient.getPushRules (/var/home/reesericci/js/node/internet-fax-machine/node_modules/matrix-js-sdk/lib/client.js:7099:22)
    at getPushRules (/var/home/reesericci/js/node/internet-fax-machine/node_modules/matrix-js-sdk/lib/sync.js:523:37)
    at SyncApi.sync (/var/home/reesericci/js/node/internet-fax-machine/node_modules/matrix-js-sdk/lib/sync.js:683:7)
    at MatrixClient.startClient (/var/home/reesericci/js/node/internet-fax-machine/node_modules/matrix-js-sdk/lib/client.js:470:18)
    at async file:///var/home/reesericci/js/node/internet-fax-machine/index.js:19:1
Waiting for saved sync before retrying push rules...
Got reply from saved sync, exists? false
ERROR

reesericci avatar May 30 '22 14:05 reesericci

import * as sdk from "matrix-js-sdk"; How are you installing matrix-js-sdk from that PR here?

t3chguy avatar May 30 '22 14:05 t3chguy

I just did npm i -S matrix-js-sdk

reesericci avatar May 30 '22 14:05 reesericci

@reesericci that'll be js-sdk v18.0.0 - not the changes in the PR.

t3chguy avatar May 30 '22 14:05 t3chguy

So then how do I install the fix? I'd like to continue using NPM if possible

reesericci avatar May 30 '22 14:05 reesericci

Either wait for the next release or install it via a github dependency https://www.pluralsight.com/guides/install-npm-packages-from-gitgithub

t3chguy avatar May 30 '22 14:05 t3chguy

What's the timeline for the next release?

reesericci avatar May 30 '22 14:05 reesericci

Next tuesday but the fix caused other issues downstream so has to be backed out. https://github.com/matrix-org/matrix-react-sdk/runs/6656911557?check_suite_focus=true

t3chguy avatar May 30 '22 14:05 t3chguy

I guess I'll wait for the release if the patch is causing issues.

Thanks!

reesericci avatar May 30 '22 14:05 reesericci

You can workaround it using

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

image

t3chguy avatar May 30 '22 14:05 t3chguy

Will do - thanks for the workaround!

reesericci avatar May 30 '22 14:05 reesericci

@t3chguy I see you reverted back changes. I've tried workarond and it works. Do I need to keep this workarond persistent?

jt0in3e avatar May 30 '22 20:05 jt0in3e

@jt0in3e see https://github.com/matrix-org/matrix-js-sdk/issues/2415#issuecomment-1141239882 for why the changes were reverted

t3chguy avatar May 30 '22 21:05 t3chguy

@t3chguy ok, will stay with workaround for now, maybe smth will change in the future. Thank you for your help

jt0in3e avatar May 31 '22 03:05 jt0in3e

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

reesericci avatar May 31 '22 03:05 reesericci

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

jt0in3e avatar May 31 '22 11:05 jt0in3e

The workaround isn't working for me when I use ESM because request only supports commonjs.

code:

import * as sdk from "matrix-js-sdk";
import matrixcs from "matrix-js-sdk";
import request from "request"

matrixcs.request(request)

// ...

error:

rollup v2.75.3
bundles src/index.ts → dist/bundle.js...
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
matrix-js-sdk (imported by src/index.ts)
[!] Error: 'default' is not exported by node_modules/request/index.js, imported by src/index.ts
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
src/index.ts (3:7)
1: import * as sdk from "matrix-js-sdk";
2: import matrixcs from "matrix-js-sdk";
3: import request from "request";
          ^
4: matrixcs.request(request);
5: const client = sdk.createClient({
Error: 'default' is not exported by node_modules/request/index.js, imported by src/index.ts
    at error (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:198:30)
    at Module.error (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:12723:16)
    at Module.traceVariable (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:13082:29)
    at ModuleScope.findVariable (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:11726:39)
    at Identifier.bind (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:8156:40)
    at CallExpression.bind (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:5360:73)
    at CallExpression.bind (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:9128:15)
    at ExpressionStatement.bind (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:5364:23)
    at Program.bind (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:5360:73)
    at Module.bindReferences (/var/home/reesericci/js/node/internet-fax-machine/node_modules/rollup/dist/shared/rollup.js:12719:18)

I'm using typescript with es2022 modules and es2020 target

reesericci avatar May 31 '22 12:05 reesericci

Has any fix to this issue been committed? I am getting the same error while trying to register a user.

EgeKaanGurkan avatar Jun 16 '22 09:06 EgeKaanGurkan

Is there any way to open a bounty on this issue? I'd be glad to throw some money your way if it meant that a fix would arrive.

reesericci avatar Jun 18 '22 02:06 reesericci

#2415 fixes this

PR 2415 seems to not exist ? is there a fix for this issue, or is the js SDK not usable at the moment ?

thank you, Andrei

andreidiaconescu avatar Jun 29 '22 14:06 andreidiaconescu

Getting this with a vitejs + preact + typescript setup as well when I try to login with password :/

MTRNord avatar Jul 14 '22 13:07 MTRNord

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

dcolley avatar Jul 19 '22 09:07 dcolley

I was using TypeScript + Rollup.js + ESM

reesericci avatar Jul 19 '22 14:07 reesericci

Is it possible there is another workaround that doesn't use the request package? I understand that work is being done to move from request to fetch but I'm just not sure how long that will take, especially since the idea of moving away from request has been around since 2018.

I don't mean to be impatient or ungrateful for the work put into projects like this, I'm just hoping to find a more immediate solution to the OP problem described here without relying on requests, if it's possible.

tearfulnumpty avatar Aug 08 '22 17:08 tearfulnumpty