You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 15, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 15, 2026. It is now read-only.
TypeError: "not a constructor" when using TypeScript #83
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'
});
Reacted by Ryan Yanulites, Alber, Kyle, Silas Hsu, Sebastian Di Luzio, Mike Ottum and Mathias Klenk
@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.
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:
importTwitterfrom"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-ignoreconsttwitter=newTwitter({consumer_key: 'XYZ',consumer_secret: 'XYZ',access_token_key: 'XYZ',access_token_secret: 'XYZ'});
To be clear, there are two necessary changes from the sample code in the comment at the top of index.d.ts:
Remove the curly brackets from the import statement
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.
Reacted by Kyle, Alber, Mathias Klenk and Yusuf Demirci
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.
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.
Activity
AlberErre commentedon Apr 23, 2020
Same issue here 🤷🏻♂️
I've added
// @ts-ignoreabove that line as a temporary workaround.danbockapps commentedon Apr 23, 2020
@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.
kyleratti commentedon Apr 23, 2020
@danbockapps I am seeing the same thing on
typescript@^3.8.3andtwitter-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-ignoreabove the client line like so:danbockapps commentedon Apr 24, 2020
To be clear, there are two necessary changes from the sample code in the comment at the top of index.d.ts:
// @ts-ignoreabove the call to create anew TwitterIn my blank project I also had to either add
"esModuleInterop": trueto thecompilerOptionsin tsconfig.json, or change the import statement toimport * as Twitter from 'twitter-lite'Then the code ran.dylanirlbeck commentedon Apr 24, 2020
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.fdebijl commentedon Apr 24, 2020
I completely overlooked
esModuleInteropwhile 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) isIf
esModuleInteropis enabled, you can also use theimport ... fromsignature, optionally with typings:Note that the
const ... requiresignature doesn't allow you to import typings from twitter-lite, so I would recommend usingesModuleInteropin any event.DalderupMaurice commentedon Apr 1, 2021
I've had this issue with version
1.1.0Had to add
esModuleInterop: trueto fix the issue, without it gives the errorI assume this PR would fix that: #139