Open
Description
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:
import { PostHogProvider } from "posthog-js/react/dist/esm/index.js";
^^^^^^^^^^^^^^^
SyntaxError: Named export 'PostHogProvider' not found. The requested module 'posthog-js/react/dist/esm/index.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'posthog-js/react/dist/esm/index.js';
const { PostHogProvider } = pkg;
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:
diff --git a/dist/es.js b/dist/es.mjs
similarity index 100%
rename from dist/es.js
rename to dist/es.mjs
diff --git a/dist/es.js.map b/dist/es.mjs.map
similarity index 100%
rename from dist/es.js.map
rename to dist/es.mjs.map
diff --git a/package.json b/package.json
index 4d35188aea0eed88a85d9c9c3a140ea10f578034..c10df882ec27ca97a29c814735f8ea53ad847dc2 100644
--- a/package.json
+++ b/package.json
@@ -23,6 +23,18 @@
},
"main": "dist/module.js",
"module": "dist/es.js",
+ "exports": {
+ ".": {
+ "types": "./dist/module.d.ts",
+ "import": "./dist/es.mjs",
+ "require": "./dist/module.js"
+ },
+ "./react": {
+ "types": "./react/dist/types/index.d.ts",
+ "import": "./react/dist/esm/index.mjs",
+ "require": "./react/dist/umd/index.js"
+ }
+ },
"types": "dist/module.d.ts",
"files": [
"lib/*",
diff --git a/react/README.md b/react/README.md
deleted file mode 100644
index 6d2737d54f58c39c85eea2afcfb6dd583686aef9..0000000000000000000000000000000000000000
diff --git a/react/dist/esm/index.js b/react/dist/esm/index.mjs
similarity index 100%
rename from react/dist/esm/index.js
rename to react/dist/esm/index.mjs
diff --git a/react/package.json b/react/package.json
index f313bf7c1e42c7d43be66390329013fd342558a0..8932551985ed093f2f2ab25ee48cdb0543b97a71 100644
--- a/react/package.json
+++ b/react/package.json
@@ -21,6 +21,13 @@
},
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
+ "exports": {
+ ".": {
+ "types": "./dist/types/index.d.ts",
+ "import": "./dist/esm/index.mjs",
+ "require": "./dist/umd/index.js"
+ }
+ },
"types": "dist/types",
"files": [
"dist/*",