HorusGoul / vite-plugin-stylex

Vite Plugin for StyleX
MIT License
116 stars 12 forks source link

Cannot find module and cannot be loaded errors #38

Closed dhutaryan closed 8 months ago

dhutaryan commented 8 months ago

I installed the plugin but it doesn't work at all.

I have an error while importing:

Cannot find module 'vite-plugin-stylex' or its corresponding type declarations.

Then I have error if I try to serve the project.

"vite-plugin-stylex" resolved to an ESM file. ESM file cannot be loaded by `require`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. [plugin externalize-deps]

I use nx. Maybe there are some specifics with that. I'm now sure.

"@stylexjs/stylex": "^0.5.1",
"vite": "^5.0.0",
"vite-plugin-stylex": "^0.5.0",
HorusGoul commented 8 months ago

The plugin is only offered in ESM format, the error you got actually mentions it and the link points to a few suggestions on how to fix it (switch your vite config file from CJS to ESM).

You could also await import('vite-plugin-stylex') by making the defineConfig async:

export default defineConfig(async () => {
  const styleX = await import('vite-plugin-stylex');

  return {
    plugins: [
      ...
      styleX(),
    ],
  }
})

My recommendation: change the config file to be an ESM module (Vite deprecated CJS on v5, so I guess they'll drop support for CJS in the future).