effect icon indicating copy to clipboard operation
effect copied to clipboard

MsgPack in @effect/platform is importing node related modules

Open bjacobso opened this issue 10 months ago • 6 comments

What version of Effect is running?

"effect": "^3.14.1" / "@effect/platform": "^0.80.1"

What steps can reproduce the bug?

Import any module from @effect/platform like: import { HttpApiBuilder } from "@effect/platform"; causes the MsgPack module that is exported to import MsgPackr which I believe has node specific imports. This is causing bundle issues with vite/cloudflare and I think breaks the expectation of exports from the @effect/platform package.

What is the expected behavior?

Not importing node related packages

What do you see instead?

11:45:36 AM [vite] (ssr) error while updating dependencies:
Error: Build failed with 7 errors:
node_modules/.pnpm/[email protected]/node_modules/detect-libc/lib/detect-libc.js:6:29: ERROR: Cannot read file: /Users/benjacobson/Development/personal/jazz-demo/child_process
node_modules/.pnpm/[email protected]/node_modules/msgpackr/node-index.js:9:30: ERROR: Cannot read file: /Users/benjacobson/Development/personal/jazz-demo/module
node_modules/.pnpm/[email protected]/node_modules/msgpackr/stream.js:1:26: ERROR: Cannot read file: /Users/benjacobson/Development/personal/jazz-demo/stream
node_modules/.pnpm/[email protected]/node_modules/node-gyp-build-optional-packages/node-gyp-build.js:1:17: ERROR: Cannot read file: /Users/benjacobson/Development/personal/jazz-demo/fs
node_modules/.pnpm/[email protected]/node_modules/node-gyp-build-optional-packages/node-gyp-build.js:2:19: ERROR: Cannot read file: /Users/benjacobson/Development/personal/jazz-demo/path
...
    at failureErrorWithLog (/Users/benjacobson/Development/personal/jazz-demo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1477:15)
    at /Users/benjacobson/Development/personal/jazz-demo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:946:25
    at /Users/benjacobson/Development/personal/jazz-demo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1355:9
    at processTicksAndRejections (node:internal/process/task_queues:105:5)

Additional information

No response

bjacobso avatar Mar 27 '25 18:03 bjacobso

Without having dug further yet, it would appear that this is an issue with vite cloudflare module resolution.

From what I can tell, the msgpackr package that is imported from MsgPack does indeed have node module imports. However! Those are correctly tugged away behind a node specific package.json entrypoint declaration.

This should not affect non-node environments unless they incorrectly resolve the node entrypoint.

Can you provide a minimal repro for me to double check this assumption?

fubhy avatar Mar 28 '25 11:03 fubhy

Here is a minimal repro of the issue: https://github.com/bjacobso/effect-cloudflare-issue

just comment or uncomment import { HttpApi } from "@effect/platform"; inside app/routes/home.tsx and then start the vite dev server. It might be more of a vite issue than a cloudflare issue?

10:36:33 AM [vite] Internal server error: Failed to load url stream (resolved id: stream) in /Users/benjacobson/Development/personal/effect-clouflare-issue/node_modules/.vite/deps_ssr/@effect_platform.js. Does the file exist?

The app is based on this starter: https://github.com/remix-run/react-router-templates/tree/main/cloudflare using the vite environments api, react router, and cloudflare

bjacobso avatar Mar 28 '25 17:03 bjacobso

So the problem is that msgpackr doesn't have a browser export condition. And the vite cloudflare plugin (or the SSR environment in vite more specifically) seem to assume a node environment and thus accepts the node condition in absence of an earlier match (conditions are evaluated on a "first match wins" basis).

There might be a way to configure this in the vite config so that it ignores the node condition. But I couldn't figure out how...

See:

  • https://vite.dev/guide/ssr
  • https://vite.dev/guide/api-environment.html

It's possible to fix this in msgpackr by adding a browser condition (see patch). Feel free to open a PR in their repo!

fubhy avatar Mar 28 '25 18:03 fubhy

I submitted a PR for your repro.

fubhy avatar Mar 28 '25 18:03 fubhy

https://github.com/kriszyp/msgpackr/pull/161

fubhy avatar Mar 28 '25 18:03 fubhy

According to the vite configuration documentation, this (or a version of this) should also work... But it doesn't:

defineConfig({
  plugins: [
    cloudflare({ viteEnvironment: { name: "ssr" } }),
  ],
  ssr: {
    target: "webworker",
    resolve: {
      // Explicitly rule out the `node` condition.
      conditions: ["worker", "browser", "default"],
      externalConditions: ["worker", "browser", "default"],
    },
  },
});

fubhy avatar Mar 29 '25 08:03 fubhy

I get the following error on bun v.1.2.22, "effect": "^3.17.14" and "@effect/platform": "^0.91.1":

> bun run --watch ./src/index.ts

1 | (function (entry, fetcher)
              ^
SyntaxError: Export named 'Msgpackr' not found in module '/home/kedom/Public/Git/mono/node_modules/.pnpm/@effect+platfo
[email protected][email protected]/node_modules/@effect/platform/dist/esm/MsgPack.js'.
      at loadAndEvaluateModule (1:11)
      at link (1:11)
      at link (1:11)
      at link (1:11)
      at link (1:11)
      at link (1:11)
      at link (1:11)
      at linkAndEvaluateModule (1:11)
      at loadAndEvaluateModule (2:1)
      at processTicksAndRejections (7:39)

kedom1337 avatar Sep 28 '25 15:09 kedom1337

I get this error in the React Router v7 app when building it

'@effect/platform': 0.92.1 effect: ^3.18.2[[ react-router: ^7.9.3 react: ^19.2.0

[react-router] The requested module '@effect/platform/MsgPack' does not provide an export named 'Msgpackr'
│ file:///Users/adrianwitaszak/CharLEEX/businesses/T6C/t6c/node_modules/.pnpm/@[email protected]_@[email protected]_effect@…
│ import { Msgpackr } from "@effect/platform/MsgPack";
│          ^^^^^^^^
│ SyntaxError: The requested module '@effect/platform/MsgPack' does not provide an export named 'Msgpackr'
│     at ModuleJob._instantiate (node:internal/modules/esm/module_job:180:21)
│     at ModuleJob.run (node:internal/modules/esm/module_job:263:5)
│     at onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:578:26)
│     at getPrerenderBuildAndHandler (/Users/adrianwitaszak/CharLEEX/businesses/T6C/t6c/node_modules/.pnpm/@[email protected]…
│     at handlePrerender (/Users/adrianwitaszak/CharLEEX/businesses/T6C/t6c/node_modules/.pnpm/@[email protected]_@react-ro…
│     at Object.handler (/Users/adrianwitaszak/CharLEEX/businesses/T6C/t6c/node_modules/.pnpm/@[email protected]_@react-rou…
│     at async Promise.all (index 0)
│     at PluginDriver.hookParallel (file:///Users/adrianwitaszak/CharLEEX/businesses/T6C/t6c/node_modules/.pnpm/[email protected]/no…
│     at file:///Users/adrianwitaszak/CharLEEX/businesses/T6C/t6c/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/s…
│     at catchUnfinishedHookActions (file:///Users/adrianwitaszak/CharLEEX/businesses/T6C/t6c/node_modules/.pnpm/[email protected]/n…
│   code: 'PLUGIN_ERROR',
│   plugin: 'react-router',
│   hook: 'writeBundle'
│ }

adding dependency '@effect/rpc': ^0.71.0 to the package made the build pass. Ofc this is a temporary fix, as I don't even use the @effect/rpc in my project..

CharLEEx avatar Oct 05 '25 09:10 CharLEEx

Same error here, I get this new error when I'm trying to upgrade:

  • effect 3.17.13 -> 3.18.2
  • @effect/platform 0.90.9 -> 0.92.1
  • @effect/platform-node 0.96.1 -> 0.98.3

I get the error SyntaxError: The requested module '@effect/platform/MsgPack' does not provide an export named 'Msgpackr' and it seems to come from the @effect/platform-node package

Image Image

wewelll avatar Oct 05 '25 10:10 wewelll

We just had a conversation in the Discord and the recommendation by @fubhy was to delete node_modules, the lock file, and pnpm store prune. This has helped to resolve that issue, even though I personally don't like fully recreating the lock file. The reason is presumably due to the pnpm store being polluted.

ChristianIvicevic avatar Oct 06 '25 12:10 ChristianIvicevic

Thanks that's what I did too, it worked, though I'm not happy with recreating the lockfile, I have a big monorepo and I'm a bit worried of side effects

wewelll avatar Oct 06 '25 13:10 wewelll