Skip to content

Conversation

kasperpeulen
Copy link

I moved cjsModuleRunnerPlugin to the minimal plugin, to see if that would solve common js issues in the browser example.

I could succesfully load marked and load this context provider from next:

import { PathnameContext } from 'next/dist/shared/lib/hooks-client-context.shared-runtime'

But it gave cryptic messages for some other nextjs modules:

import { AppRouterContext } from 'next/dist/shared/lib/app-router-context.shared-runtime'
import { publicAppRouterInstance } from 'next/dist/client/components/app-router-instance'

The error is:

app-router-context.s…me.js?v=9591181e:35 Uncaught TypeError: Cannot read properties of undefined (reading '_')
    at eval (app-router-context.s…js?v=9591181e:35:57)
    at async ESModulesEvaluator.runInlinedModule (module-runner.js?v=8935b260:905:3)
    at async ModuleRunner.directRequest (module-runner.js?v=8935b260:1112:59)
    at async ModuleRunner.cachedRequest (module-runner.js?v=8935b260:1030:73)
    at async eval (ContextProvider.tsx:6:31)
    at async ESModulesEvaluator.runInlinedModule (module-runner.js?v=8935b260:905:3)
    at async ModuleRunner.directRequest (module-runner.js?v=8935b260:1112:59)
    at async ModuleRunner.cachedRequest (module-runner.js?v=8935b260:1030:73)

It seems that the following module doesn't get loaded correctly:

const _interop_require_default = (await __vite_ssr_dynamic_import__("/@fs/Users/kasperpeulen/code/github/vite-plugin-react/node_modules/.pnpm/@[email protected]/node_modules/@swc/helpers/esm/_interop_require_default.js?v=9591181e")).default;

So that the _ method is not defined:

const _react = /* @__PURE__ */
    _interop_require_default._((await __vite_ssr_dynamic_import__('/node_modules/.vite/deps_react_client/react.js?v=9591181e').then(m => ( (m$2) => m$2?.__esModule ? m$2 : {

@kasperpeulen kasperpeulen changed the title Example of loading commonjs modules from marked and next Loading commonjs modules in the browser Oct 7, 2025
@kasperpeulen kasperpeulen changed the title Loading commonjs modules in the browser Reproduction of loading commonjs modules from nextjs in the browser Oct 7, 2025
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Oct 7, 2025

Thanks for the repro. This looks like one of currently expected bugs in cjsModuleRunnerPlugin. I think some cjs module has require("@swc/helpers") and that's probably transformed to (await import(".../@swc/helpers/esm/...")).default, but this needs to be avoided somehow (probably either import(".../@swc/helpers/esm/...") without default or resolve it to import(".../@swc/helpers/cjs/...") + default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants