-
Notifications
You must be signed in to change notification settings - Fork 159
missing pkg.exports
in package.json
#908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Bumping this as it's still an issue |
We are currently experiencing this issue as well when we upgraded our Remix project to Vite |
I used In short, I added an 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 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 |
@ekojsalim Can you post a code snippet to illustrate your solution? |
Something like this:
We use jotai instead of more usual context/provider scheme so it might be a bit different. Baiscally, it's just taking the existing |
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 } |
Can anyone from Posthog confirm if this is on their radar and if there's a plan to fix it? In the year the issue has been open, we've seen #1168 (autoclosed due to inactivity), #1284 (autoclosed due to inactivity), and #1293 (autoclosed due to inactivity). Meanwhile, #1450 has also been raised, and PostHog/posthog.com#9830 proposes telling users to manually reimplement much of |
This is a problem for me too. Here's what it looks like in a production-like app: https://github.com/iloveitaly/python-starter-template/blob/e36a3013b2a84d8c7807b38b55550d672bf78d41/web/app/configuration/posthog.tsx#L25-L30 |
Still happening, were in 2025! |
Seriously annoying. Cant deploy without this. |
Wow, a show stopper that's been around for 1 year. We are unable to use Posthog with Astro for this reason. @rafaeelaudibert can you help? |
How come this is not resolved after a year |
Not sure if it's the right solution for other imports, but managed to fix my issue with imports from diff --git a/package.json b/package.json
index 5cd394f287ba6ff4d707f50c22ab187c08d6dcb4..58a772f22ca12bf6ce0c91d8b6c5fe3d378223a7 100644
--- a/package.json
+++ b/package.json
@@ -37,6 +37,18 @@
"react/dist/*",
"react/package.json"
],
+ "exports": {
+ ".": {
+ "types": "./dist/module.d.ts",
+ "import": "./dist/module.js",
+ "require": "./dist/main.js"
+ },
+ "./react": {
+ "types": "./react/dist/types/index.d.ts",
+ "import": "./react/dist/esm/index.js",
+ "require": "./react/dist/umd/index.js"
+ }
+ },
"dependencies": {
"core-js": "^3.38.1",
"fflate": "^0.4.8",
diff --git a/react/package.json b/react/package.json
index 5c2d9042056b3055c961cac920602c15fc3512f3..88afa41d6690b9951019235255660a442973ed0e 100644
--- a/react/package.json
+++ b/react/package.json
@@ -36,6 +36,13 @@
"optional": true
}
},
+ "exports": {
+ ".": {
+ "types": "./dist/types/index.d.ts",
+ "import": "./dist/esm/index.js",
+ "require": "./dist/umd/index.js"
+ }
+ },
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-inject": "^4.0.2", EDIT: This is for versions older than originally posted in the issue, the fix from #908 (comment) works properly for the latest version ( |
Uh oh!
There was an error while loading. Please reload this page.
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/[email protected]) 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:
The text was updated successfully, but these errors were encountered: