PostHog / posthog-node

Official PostHog Node library
MIT License
21 stars 19 forks source link

Uncaught TypeError: posthog_node_1.default is not a constructor #50

Closed aacanakin closed 2 years ago

aacanakin commented 3 years ago

I'm using "typescript": "^3.6.3" with the following tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "inlineSources": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  },
  "exclude": ["node_modules", "dist"]
}

When I try to create a new instance of PostHog it gives the following error;

➜  posthog-bug ts-node
> import PostHog from 'posthog-node'

> const client = new PostHog('')
/Users/aras/Work/posthog-bug/<repl>.ts:2
const client = new posthog_node_1.default('');
               ^

Uncaught TypeError: posthog_node_1.default is not a constructor
    at /Users/aras/Work/posthog-bug/<repl>.ts:2:16
    at Script.runInThisContext (vm.js:134:12)
    at runInContext (/Users/aras/.nvm/versions/node/v14.17.6/lib/node_modules/ts-node/src/repl.ts:574:19)
    at Object.execCommand (/Users/aras/.nvm/versions/node/v14.17.6/lib/node_modules/ts-node/src/repl.ts:540:28)
    at /Users/aras/.nvm/versions/node/v14.17.6/lib/node_modules/ts-node/src/repl.ts:562:47
    at Array.reduce (<anonymous>)
    at appendCompileAndEvalInput (/Users/aras/.nvm/versions/node/v14.17.6/lib/node_modules/ts-node/src/repl.ts:562:23)
    at evalCodeInternal (/Users/aras/.nvm/versions/node/v14.17.6/lib/node_modules/ts-node/src/repl.ts:211:12)
    at REPLServer.nodeEval (/Users/aras/.nvm/versions/node/v14.17.6/lib/node_modules/ts-node/src/repl.ts:233:26)
    at bound (domain.js:416:15)
>

When I try to log PostHog, it is undefined

Twixes commented 3 years ago

Hey! Could you share a reproducible code example of the error? That would make it much easier to solve the issue.

aacanakin commented 3 years ago

Hi. I was using with nestjs but it doesn't work without it either. The following steps will give the error with "typescript": "^3.6.3";

  1. Use this tsconfig.json

    {
    "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "inlineSources": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
    },
    "exclude": ["node_modules", "dist"]
    }
  2. Install & run ts-node

  3. Run

    import PostHog from 'posthog-node'
    const client = new PostHog('test-key')

    That's it. You'll see the error

aacanakin commented 3 years ago

It also doesn't work with the "typescript": "4.2.3" as well. See https://codesandbox.io/s/icy-monad-fz8ke?file=/src/index.ts

Alkasai commented 2 years ago

The entire type definitions file seem off, for example groupIdentify should have a callback as the last param. Also the default export should probably be top level and not inside a declaration. How was this generated? Can we generate a new index.d.ts?

mattinannt commented 2 years ago

Had the same error. Posthog instance during setup told me to import

import PostHog from 'posthog-node'

but docs say it's

import { PostHog } from 'posthog-node'

what also seems to work for me :-)

neilkakkar commented 2 years ago

Hey @matthiasnannt , could you point me to where you were told to do the former?

We recently updated docs to say the latter, will update the remaining place as well 😅 . The type definitions were sorted out in version 2.0

mattinannt commented 2 years ago

@neilkakkar I installed posthog on my server using the instructions for Hobby. It seems because it was pulling the latest release ( 1.38.1 ) that in this version the text wasn't already changed. But gladly you changed it yesterday so it will be fine in the next release 💪

neilkakkar commented 2 years ago

Perfect, thanks!