-
Notifications
You must be signed in to change notification settings - Fork 174
Description
I'm using the SSG approach that the Vite docs link to, which produces an ESNext build that is then reimported for pre-rendering routes. After adding in react-helmet-async, this approach fails with
import { HelmetProvider } from "react-helmet-async";
^^^^^^^^^^^^^^
SyntaxError: Named export 'HelmetProvider' not found. The requested module 'react-helmet-async' 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 'react-helmet-async';
const { HelmetProvider } = pkg;
I tried all the variations of defining the import (default
, * as
) and none of these had any effect.
Workaround
Vite has a ssr.noexternal config, which fixed it for me.
export default defineConfig({
plugins: [react()],
ssr: {
noExternal: ['react-helmet-async'],
},
});
I also played about with react-helment-async's package.json
locally to point the main
at the esm build this resolved the issue for me. I also separately defined this package as "type": "module"
and specified the "exports"
field and this also worked. So I think there's a referencing config that isn't playing nicely with the vite ssg approach.
The vite config is fine for me now so I'm not going to look into this further, but hopefully this issue helps someone else looking in the future.