Open tgds opened 11 months ago
Bumping this as it's still an issue
@benjackwhite 🙏
We are currently experiencing this issue as well when we upgraded our Remix project to Vite
I used yarn patch
to workaround this issue locally.
In short, I added an exports
field to the main package file and renamed the ESM files from js
to mjs
.
This seems to have done the trick.
posthog-js-npm-1.157.2-d8e62a83b4.patch
I do think that this should be a relatively straightforward fix... it's just a case of aligning your package file with the ESM standards as documented in the Node JS docs.
However, I will not that creating a hybrid package (i.e. ESM and CJS) is a huge PITA, but using .cjs
and .mjs
and including the appropriate exports
data should do it.
I'd offer a PR, but I haven't used Rollup, so I'm probably not the best person to do it.
@dancrumb That isn’t far off from what we’re doing at my job. There’s a little bit of extra work we have to do to make it work on our npm workspaces codebase. The main problem is that our local fix could break in a later version of posthog, and we try to keep everything pretty up to date in our app.
Encountering the same issue with posthog-js/react
and Remix + Vite. Since I'm not using the feature flag feature, I found it simpler to just reimplement PostHogProvider
and usePostHog
. That should be more 'future-proof'.
@ekojsalim Can you post a code snippet to illustrate your solution?
@ekojsalim Can you post a code snippet to illustrate your solution?
Something like this:
import { atom, useAtomValue, useSetAtom } from "jotai";
import posthog from "posthog-js";
import { useEffect } from "react";
const postHogAtom = atom<typeof posthog | undefined>(undefined);
const PostHogProvider = ({
children,
options,
apiKey,
}: { children: React.ReactNode; options: any; apiKey: string }) => {
const setPostHogInstance = useSetAtom(postHogAtom);
useEffect(() => {
const posthogInstance = posthog.init(apiKey, options);
setPostHogInstance(posthogInstance);
}, [apiKey, options, setPostHogInstance]);
return children;
};
const usePostHog = () => useAtomValue(postHogAtom);
export { PostHogProvider, usePostHog };
We use jotai instead of more usual context/provider scheme so it might be a bit different. Baiscally, it's just taking the existing PostHogProvider
and usePostHog
and reimplementing it in the internal codebase. Importing posthog-js
is fine but posthog-js/react
breaks.
Having this issue too. Took down our site first time tried to install post hog. Vite is very popular bundler. Would be nice to have support.
How is this issue almost a year old?? They even showed you how to fix it in the second reply
Anyway, here's my solution. It's just ekojsalim's without the jotai dependency:
import posthog from 'posthog-js'
import React, { createContext, useContext, useEffect, useState } from 'react'
type PostHogType = typeof posthog | undefined
const PostHogContext = createContext<PostHogType>(undefined)
interface PostHogProviderProps {
children: React.ReactNode
options?: any
apiKey?: string
}
const PostHogProvider: React.FC<PostHogProviderProps> = ({
children,
options,
apiKey,
}) => {
const [posthogInstance, setPosthogInstance] = useState<PostHogType>(undefined)
useEffect(() => {
if (apiKey) {
const instance = posthog.init(apiKey, options)
setPosthogInstance(instance)
} else {
// If apiKey is undefined, ensure PostHog instance is not set
setPosthogInstance(undefined)
}
}, [apiKey, options])
return (
<PostHogContext.Provider value={posthogInstance}>
{children}
</PostHogContext.Provider>
)
}
const usePostHog = () => useContext(PostHogContext)
export { PostHogProvider, usePostHog }
It is not possible to use
posthog-js
package within an ESM project in Node environment, because the package.json is missing the exports property (https://publint.dev/posthog-js@1.92.1) while Node doesn't read the module property and thus it fails to treat it as an ES package.You get the following error if you try:
This is within a Remix Vite project, but I guess it's relevant for any ESM project that runs SSR in Node and doesn't prebundle dependencies.
I had to apply the following patch to get it working: