Skip to content
This repository was archived by the owner on Sep 15, 2026. It is now read-only.
This repository was archived by the owner on Sep 15, 2026. It is now read-only.

TypeError: "not a constructor" when using TypeScript #83

Closed

Description

@danbockapps

I get this error when using the example code at the top of index.d.ts:

Users-MacBook-Pro:twitter-lite-test danbock$ ts-node dev.ts 

/Users/danbock/code/twitter-lite-test/dev.ts:3
const twitter = new Twitter({
                ^
TypeError: twitter_lite_1.Twitter is not a constructor
    at Object.<anonymous> (/Users/danbock/code/twitter-lite-test/dev.ts:3:17)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Module.m._compile (/Users/danbock/.nvm/versions/node/v12.16.2/lib/node_modules/ts-node/src/index.ts:836:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/danbock/.nvm/versions/node/v12.16.2/lib/node_modules/ts-node/src/index.ts:839:12)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at main (/Users/danbock/.nvm/versions/node/v12.16.2/lib/node_modules/ts-node/src/bin.ts:226:14)
    at Object.<anonymous> (/Users/danbock/.nvm/versions/node/v12.16.2/lib/node_modules/ts-node/src/bin.ts:485:3)

I reproduced this on a blank project. Just ran npm install twitter-lite and copied this code into a blank file and ran it using ts-node. Got the same error.

import { Twitter } from 'twitter-lite';

const twitter = new Twitter({
 consumer_key: 'XYZ',
 consumer_secret: 'XYZ',
 access_token_key: 'XYZ',
 access_token_secret: 'XYZ'
});

Activity

  1. AlberErre commented on Apr 23, 2020

    @AlberErre

    Same issue here 🤷🏻‍♂️

    I've added // @ts-ignore above that line as a temporary workaround.

  2. danbockapps commented on Apr 23, 2020

    @danbockapps
    Author

    @AlberErre Above what line? I believe that TypeScript compilation is actually happening without errors, and the TypeError I'm seeing is a runtime JavaScript error.

  3. kyleratti commented on Apr 23, 2020

    @kyleratti

    @danbockapps I am seeing the same thing on typescript@^3.8.3 and twitter-lite@^0.10.1-0. I think this has to do with the typings exporting the entire namespace while the actual library exports the Twitter class.

    I was a bit confused by the suggestion from @AlberErre as well, but I realized he meant he added // @ts-ignore above the client line like so:

    import Twitter from "twitter-lite"; // typings and runtime OK for this line
    
    ...
    
    // changing your import to match above will trigger 'This expression is not constructable.'
    // the @ts-ignore flag will let TS compile anyway
    // @ts-ignore
    const twitter = new Twitter({
     consumer_key: 'XYZ',
     consumer_secret: 'XYZ',
     access_token_key: 'XYZ',
     access_token_secret: 'XYZ'
    });
  4. danbockapps commented on Apr 24, 2020

    @danbockapps
    Author

    To be clear, there are two necessary changes from the sample code in the comment at the top of index.d.ts:

    1. Remove the curly brackets from the import statement
    2. Add // @ts-ignore above the call to create a new Twitter

    In my blank project I also had to either add "esModuleInterop": true to the compilerOptions in tsconfig.json, or change the import statement to import * as Twitter from 'twitter-lite' Then the code ran.

  5. dylanirlbeck commented on Apr 24, 2020

    @dylanirlbeck
    Contributor

    Hi all, sorry you're seeing these errors in TypeScript. We just added typings to the library, so there was bound to be some bugs 😄 That said, I'd encourage all of you to take a look to see if you can find the issue and open a PR!

    I'm going to cc @fdebijl since he made the PR for types originally. I'm no TS expert so hopefully we can work together to get this fixed before the next release of twitter-lite.

  6. fdebijl commented on Apr 24, 2020

    @fdebijl
    Contributor

    I completely overlooked esModuleInterop while developing the typings, I'm submitting a PR with a fix as we speak.

    Following this PR, the safe import signature (which will work without esModuleInterop) is

    const Twitter = require('twitter-lite')
    

    If esModuleInterop is enabled, you can also use the import ... from signature, optionally with typings:

    import Twitter from 'twitter-lite';
    import Twitter, { TwitterOptions, AccessTokenOptions } from 'twitter-lite';
    

    Note that the const ... require signature doesn't allow you to import typings from twitter-lite, so I would recommend using esModuleInterop in any event.

  7. DalderupMaurice commented on Apr 1, 2021

    @DalderupMaurice

    I've had this issue with version 1.1.0
    Had to add esModuleInterop: true to fix the issue, without it gives the error

    I assume this PR would fix that: #139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions