Closed cwkosman closed 5 months ago
Hey 👋
This is a known issue (1, 2) when running on a worker environment such as MiniOxygen, which is what the oxygen()
plugin is adding.
So far, you need to add these CJS dependencies manually to your Vite config in the ssr.optimizeDeps.include
array.
We are soon releasing a tool to do this automatically: https://github.com/Shopify/hydrogen/pull/2106 Hopefully at some point Vite itself will be able to discover these CJS dependencies for the server (right now it only does it for the browser because the server is normally Node.js).
ssr.optimizeDeps.include
did the trick. Thanks @frandiox!
What is the location of your example repository?
https://github.com/ninetailed-inc/ninetailed-hydrogen
Which package or tool is having this issue?
Hydrogen
What version of that package or tool are you using?
2024.4.2
What version of Remix are you using?
^2.8.0
Steps to Reproduce
@ninetailed/experience.js-react
andi18n-iso-countries
as examples.import {NinetailedProvider} from '@ninetailed/experience.js-react'
orimport {getAlpha2Codes} from 'i18n-iso-countries'
npm run dev
ReferenceError: require is not defined
error.Expected Behavior
The Vite bundler should be able to handle CommonJS dependencies (https://vitejs.dev/guide/dep-pre-bundling#the-why | https://shopify.engineering/developer-experience-with-hydrogen-and-vite), and the project would boot in dev mode by using Vite's depedency pre-bundling. A test case of spinning up a new non-Hydrogen Remix project produces the expected behaviour where modules with CommonJS syntax are handled without error: https://github.com/ninetailed-inc/ninetailed-remix
Actual Behavior
Error output to console. Full error log:
Note that the Ninetailed module itself uses ESM syntax to export NinetailedProvider. The error throws on it's dependency
i18n-iso-countries
, which uses CJS. This is also reproducible by importing functions fromi18n-iso-countries
directly.