question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: "not a constructor" when using TypeScript

See original GitHub issue

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'
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
danbockappscommented, 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:

  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.

3reactions
fdebijlcommented, Apr 27, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Does This Typescript Output "[Class] is not a constructor."?
This error message means that [Class] is not initialized by the time a call to its constructor is ...
Read more >
TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >
TypeError: "X" is not a constructor in JavaScript | bobbyhadz
To solve the "TypeError: 'X' is not a constructor" in JavaScript, make sure to only use the new operator on valid constructors, e.g....
Read more >
Resolving TypeError: "X" is Not a Constructor in JavaScript
JavaScript "TypeError: "x" is not a constructor" errors occur when invalid objects or a variable is erroneously used as a constructor.
Read more >
Typescript error "class is not a constructor" #8910 - GitHub
I am running the following typescript code in the ES6 target environment and it says that "Cars is not a constructor".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found